Exemplo n.º 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;
  }
Exemplo n.º 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);
    }
  }
Exemplo n.º 3
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;
  }