Exemplo n.º 1
0
  protected ResultSet getValues(
      RDBMSInformationTarget rdbmsInformationTarget, Statement statement, String identifier)
      throws SQLException {
    AbstactDialect dialect = getDialect(rdbmsInformationTarget);
    if (dialect == null) return null;

    String query =
        "SELECT "
            + dialect.getSQLColumnNamePrefix()
            + rdbmsInformationTarget.getColumnName()
            + dialect.getSQLColumnNameSuffix();

    if (identifier != null)
      query +=
          "," + dialect.getSQLColumnNamePrefix() + identifier + dialect.getSQLColumnNameSuffix();

    query +=
        " FROM "
            + dialect.getSQLTableNamePrefix()
            + rdbmsInformationTarget.getTableName()
            + dialect.getSQLTableNameSuffix();

    statement.executeQuery(query);
    try {
      return statement.getResultSet();
    } catch (Exception e) {
      logger.error("Error occured when getting result set", e);
      return null;
    }
  }
Exemplo n.º 2
0
 @Override
 public List<String> getSampleValues(RDBMSInformationTarget rdbmsInformationTarget) {
   List<String> returnList = new LinkedList<String>();
   Connection connection = null;
   Statement statement = null;
   ResultSet rs = null;
   try {
     connection = getSQLConnection(rdbmsInformationTarget.getRdbmsConnection());
     statement = connection.createStatement();
     rs = getValues(rdbmsInformationTarget, statement, null);
     while (rs.next()) {
       Object obj = rs.getObject(1);
       String stringValue = obj.toString();
       returnList.add(stringValue);
       if (returnList.size() == 10) break;
     }
   } catch (ClassNotFoundException e) {
     logger.error("Probably JDBC driver is not found", e);
   } catch (SQLException e) {
     logger.error("An error occured during establishing connection and getting sample values", e);
   } finally {
     try {
       if (connection != null && !connection.isClosed()) connection.close();
       if (statement != null && !statement.isClosed()) statement.close();
       if (rs != null && !rs.isClosed()) rs.close();
     } catch (SQLException e) {
       logger.error("Error occurred when closing sql objects", e);
     }
   }
   return returnList;
 }
Exemplo n.º 3
0
 protected String getIdentifier(
     RDBMSInformationTarget rdbmsInformationTarget, Connection connection) throws SQLException {
   ResultSet rs = null;
   String identifier = null;
   try {
     rs =
         connection
             .getMetaData()
             .getPrimaryKeys(
                 rdbmsInformationTarget.getCatalogName(),
                 rdbmsInformationTarget.getSchemaName(),
                 rdbmsInformationTarget.getTableName());
     if (rs.next()) {
       identifier = rs.getString("COLUMN_NAME");
       if (rs.next()) // currently we do not support more than one pk;
       return null;
     }
     return identifier;
   } catch (SQLException e) {
     throw e;
   } finally {
     if (rs != null && !rs.isClosed()) rs.close();
   }
 }
Exemplo n.º 4
0
 protected AbstactDialect getDialect(RDBMSInformationTarget rdbmsInformationTarget) {
   return getDialect(rdbmsInformationTarget.getRdbmsConnection());
 }
Exemplo n.º 5
0
  public void enumerateFun(final Integer rdbmsInformationTargetId, AbstractEntity entity) {
    final RDBMSObjectEnumProxy enumProxy = getEnumProxy(entity);
    if (enumProxy == null) return;

    Connection connection = null;
    Statement statement = null;
    ResultSet rs = null;
    try {
      final RDBMSInformationTarget rdbmsInformationTarget =
          transactionTemplate.execute(
              new TransactionCallback<RDBMSInformationTarget>() {
                @Override
                public RDBMSInformationTarget doInTransaction(TransactionStatus arg0) {
                  rdbmsConnectionDAO.startProcess(rdbmsInformationTargetId);
                  return rdbmsConnectionDAO.getInformationTargetById(rdbmsInformationTargetId);
                }
              });
      entity = enumProxy.refresh(rdbmsInformationTarget, entity);

      connection = getSQLConnection(rdbmsInformationTarget.getRdbmsConnection());
      final String identifier = getIdentifier(rdbmsInformationTarget, connection);
      Boolean incrementalEnum = (identifier != null);
      if (!incrementalEnum) {
        transactionTemplate.execute(
            new TransactionCallbackWithoutResult() {
              @Override
              protected void doInTransactionWithoutResult(TransactionStatus arg0) {
                rdbmsConnectionDAO.deleteValues(rdbmsInformationTarget);
              }
            });
        enumProxy.truncate(rdbmsInformationTarget, entity);
      }
      statement = connection.createStatement();
      rs = getValues(rdbmsInformationTarget, statement, identifier);
      while (rs.next()) {
        Object obj = rs.getObject(1);
        String stringValue = obj.toString();

        Boolean isValid = enumProxy.isValid(rdbmsInformationTarget, entity, stringValue);
        if (!isValid) continue;
        final int stringHashCode = stringValue.hashCode();

        Boolean valueIsAlreadyStored =
            transactionTemplate.execute(
                new TransactionCallback<Boolean>() {
                  @Override
                  public Boolean doInTransaction(TransactionStatus arg0) {
                    return rdbmsConnectionDAO.hasValue(rdbmsInformationTarget, stringHashCode);
                  }
                });

        if (!incrementalEnum && valueIsAlreadyStored) continue;

        RDBMSEnumeratedValue ev = null;
        String idValue = null;
        if (incrementalEnum) {
          Object idObj = rs.getObject(2);
          idValue = idObj.toString();
          final String idValueF = idValue;
          ev =
              transactionTemplate.execute(
                  new TransactionCallback<RDBMSEnumeratedValue>() {
                    @Override
                    public RDBMSEnumeratedValue doInTransaction(TransactionStatus arg0) {
                      return rdbmsConnectionDAO.getValue(rdbmsInformationTarget, idValueF);
                    }
                  });

          if (ev == null && valueIsAlreadyStored) continue;
          if (ev != null && ev.getHashCode() == stringHashCode)
            continue; // we already have this value
          valueIsAlreadyStored =
              transactionTemplate.execute(
                  new TransactionCallback<Boolean>() {
                    @Override
                    public Boolean doInTransaction(TransactionStatus arg0) {
                      return rdbmsConnectionDAO.hasOtherValue(
                          rdbmsInformationTarget, stringHashCode, idValueF);
                    }
                  });
        }

        if (valueIsAlreadyStored && ev != null && ev.getId() != null) {
          final RDBMSEnumeratedValue evF = ev;
          transactionTemplate.execute(
              new TransactionCallbackWithoutResult() {
                @Override
                protected void doInTransactionWithoutResult(TransactionStatus arg0) {
                  rdbmsConnectionDAO.remove(evF);
                }
              });
          enumProxy.delete(rdbmsInformationTarget, entity, idValue);
          continue;
        }

        if (ev == null) {
          ev = new RDBMSEnumeratedValue();
          ev.setInformationTarget(rdbmsInformationTarget);
          ev.setOriginalId(idValue);
        }

        ev.setHashCode(stringHashCode);
        if (enumProxy.shouldStoreValue()) ev.setString(stringValue);
        {
          final RDBMSEnumeratedValue evF = ev;
          transactionTemplate.execute(
              new TransactionCallbackWithoutResult() {
                @Override
                protected void doInTransactionWithoutResult(TransactionStatus arg0) {
                  rdbmsConnectionDAO.save(evF);
                }
              });
        }
        enumProxy.save(rdbmsInformationTarget, entity, idValue, stringValue);
      }
    } catch (ClassNotFoundException e) {
      logger.error("Probably JDBC driver is not found", e);
    } catch (SQLException e) {
      logger.error(
          "An error occured during establishing connection and getting results of query", e);
    } finally {
      transactionTemplate.execute(
          new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus arg0) {
              rdbmsConnectionDAO.finalizeProcess(rdbmsInformationTargetId);
            }
          });
      try {
        if (connection != null && !connection.isClosed()) connection.close();
        if (statement != null && !statement.isClosed()) statement.close();
        if (rs != null && !rs.isClosed()) rs.close();
      } catch (SQLException e) {
        logger.error("Error occurred when closing sql objects", e);
      }
    }
  }