Пример #1
0
  private String[] getPrimaryKeyNames() throws SQLException {
    logger.debug("getPrimaryKeyNames() - start");

    String schemaName = _qualifiedTableNameSupport.getSchema();
    String tableName = _qualifiedTableNameSupport.getTable();

    Connection connection = _connection.getConnection();
    DatabaseMetaData databaseMetaData = connection.getMetaData();

    DatabaseConfig config = _connection.getConfig();
    IMetadataHandler metadataHandler =
        (IMetadataHandler) config.getProperty(DatabaseConfig.PROPERTY_METADATA_HANDLER);

    ResultSet resultSet = metadataHandler.getPrimaryKeys(databaseMetaData, schemaName, tableName);

    List list = new ArrayList();
    try {
      while (resultSet.next()) {
        String name = resultSet.getString(4);
        int sequence = resultSet.getInt(5);
        list.add(new PrimaryKeyData(name, sequence));
      }
    } finally {
      resultSet.close();
    }

    Collections.sort(list);
    String[] keys = new String[list.size()];
    for (int i = 0; i < keys.length; i++) {
      PrimaryKeyData data = (PrimaryKeyData) list.get(i);
      keys[i] = data.getName();
    }

    return keys;
  }
Пример #2
0
  /**
   * Creates a new database table metadata
   *
   * @param tableName The name of the table - can be fully qualified
   * @param connection The database connection
   * @param validate Whether or not to validate the given input data. It is not recommended to set
   *     the validation to <code>false</code> because it is then possible to create an instance of
   *     this object for a db table that does not exist.
   * @param caseSensitiveMetaData Whether or not the metadata looked up in a case sensitive way
   * @throws DataSetException
   * @since 2.4.1
   */
  DatabaseTableMetaData(
      final String tableName,
      IDatabaseConnection connection,
      boolean validate,
      boolean caseSensitiveMetaData)
      throws DataSetException {
    if (tableName == null) {
      throw new NullPointerException("The parameter 'tableName' must not be null");
    }
    if (connection == null) {
      throw new NullPointerException("The parameter 'connection' must not be null");
    }

    _connection = connection;
    _caseSensitiveMetaData = caseSensitiveMetaData;

    try {
      Connection jdbcConnection = connection.getConnection();
      if (!caseSensitiveMetaData) {
        _originalTableName = SQLHelper.correctCase(tableName, jdbcConnection);
        SQLHelper.logDebugIfValueChanged(
            tableName, _originalTableName, "Corrected table name:", DatabaseTableMetaData.class);
      } else {
        _originalTableName = tableName;
      }

      // qualified names support - table name and schema is stored here
      _qualifiedTableNameSupport =
          new QualifiedTableName(_originalTableName, _connection.getSchema());

      if (validate) {
        String schemaName = _qualifiedTableNameSupport.getSchema();
        String plainTableName = _qualifiedTableNameSupport.getTable();
        logger.debug(
            "Validating if table '{}' exists in schema '{}' ...", plainTableName, schemaName);
        try {
          DatabaseConfig config = connection.getConfig();
          IMetadataHandler metadataHandler =
              (IMetadataHandler) config.getProperty(DatabaseConfig.PROPERTY_METADATA_HANDLER);
          DatabaseMetaData databaseMetaData = jdbcConnection.getMetaData();
          if (!metadataHandler.tableExists(databaseMetaData, schemaName, plainTableName)) {
            throw new NoSuchTableException(
                "Did not find table '" + plainTableName + "' in schema '" + schemaName + "'");
          }
        } catch (SQLException e) {
          throw new DataSetException(
              "Exception while validation existence of table '" + plainTableName + "'", e);
        }
      } else {
        logger.debug("Validation switched off. Will not check if table exists.");
      }
    } catch (SQLException e) {
      throw new DataSetException(
          "Exception while retrieving JDBC connection from dbunit connection '" + connection + "'",
          e);
    }
  }
Пример #3
0
  public Column[] getPrimaryKeys() throws DataSetException {
    logger.debug("getPrimaryKeys() - start");
    DatabaseConfig config = _connection.getConfig();
    IColumnFilter primaryKeysFilter =
        (IColumnFilter) config.getProperty(DatabaseConfig.PROPERTY_PRIMARY_KEY_FILTER);

    if (_primaryKeys == null || primaryKeyFilterChanged(primaryKeysFilter)) {
      try {
        lastKeyFilter = primaryKeysFilter;
        if (primaryKeysFilter != null) {
          _primaryKeys = Columns.getColumns(getTableName(), getColumns(), primaryKeysFilter);
        } else {
          String[] pkNames = getPrimaryKeyNames();
          _primaryKeys = Columns.getColumns(pkNames, getColumns());
        }
      } catch (SQLException e) {
        throw new DataSetException(e);
      }
    }
    return _primaryKeys;
  }
Пример #4
0
 /**
  * Configures the given statement so that it has the properties that are configured in this
  * {@link DatabaseConfig}.
  *
  * @param stmt The statement to be configured.
  * @throws SQLException
  * @since 2.4.4
  */
 void configureStatement(Statement stmt) throws SQLException {
   logger.trace("configureStatement(stmt={}) - start", stmt);
   Integer fetchSize = (Integer) config.getProperty(DatabaseConfig.PROPERTY_FETCH_SIZE);
   stmt.setFetchSize(fetchSize.intValue());
   logger.debug("Statement fetch size set to {}", fetchSize);
 }
Пример #5
0
  public Column[] getColumns() throws DataSetException {
    logger.debug("getColumns() - start");

    if (_columns == null) {
      try {
        // qualified names support
        String schemaName = _qualifiedTableNameSupport.getSchema();
        String tableName = _qualifiedTableNameSupport.getTable();

        Connection jdbcConnection = _connection.getConnection();
        DatabaseMetaData databaseMetaData = jdbcConnection.getMetaData();

        DatabaseConfig config = _connection.getConfig();

        IMetadataHandler metadataHandler =
            (IMetadataHandler) config.getProperty(DatabaseConfig.PROPERTY_METADATA_HANDLER);
        ResultSet resultSet = metadataHandler.getColumns(databaseMetaData, schemaName, tableName);

        try {
          IDataTypeFactory dataTypeFactory = super.getDataTypeFactory(_connection);
          boolean datatypeWarning = config.getFeature(DatabaseConfig.FEATURE_DATATYPE_WARNING);

          List columnList = new ArrayList();
          while (resultSet.next()) {
            // Check for exact table/schema name match because
            // databaseMetaData.getColumns() uses patterns for the lookup
            boolean match =
                metadataHandler.matches(resultSet, schemaName, tableName, _caseSensitiveMetaData);
            if (match) {
              Column column = SQLHelper.createColumn(resultSet, dataTypeFactory, datatypeWarning);
              if (column != null) {
                columnList.add(column);
              }
            } else {
              logger.debug(
                  "Skipping <schema.table> '"
                      + resultSet.getString(2)
                      + "."
                      + resultSet.getString(3)
                      + "' because names do not exactly match.");
            }
          }

          if (columnList.size() == 0) {
            logger.warn(
                "No columns found for table '"
                    + tableName
                    + "' that are supported by dbunit. "
                    + "Will return an empty column list");
          }

          _columns = (Column[]) columnList.toArray(new Column[0]);
        } finally {
          resultSet.close();
        }
      } catch (SQLException e) {
        throw new DataSetException(e);
      }
    }
    return _columns;
  }