All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nocrala.tools.database.sentinel.adapters.PostgreSQLAdapter Maven / Gradle / Ivy

package org.nocrala.tools.database.sentinel.adapters;

import java.sql.DatabaseMetaData;
import java.sql.SQLException;

import org.nocrala.tools.database.sentinel.DatabaseTypedIdentifier;

public class PostgreSQLAdapter implements DatabaseAdapter {

  @SuppressWarnings("unused")
  private DatabaseMetaData dm;
  private String quotingDelimiter;

  public PostgreSQLAdapter(final DatabaseMetaData dm) throws SQLException {
    this.dm = dm;
    this.quotingDelimiter = dm.getIdentifierQuoteString();
  }

  @Override
  public boolean areEquivalentIdentifiers(final String jdbcName, final DatabaseTypedIdentifier identifier) {
    if (identifier.isQuoted()) {
      return jdbcName.equals(identifier.getName());
    } else {
      return jdbcName.equals(identifier.getName().toLowerCase());
    }
  }

  private static final String UNQUOTED_OBJECT_NAME_PATTERN = "[a-z][a-z0-9_]*";

  @Override
  public String renderObjectName(final String name) {
    if (name == null) {
      return null;
    }
    if (name.matches(UNQUOTED_OBJECT_NAME_PATTERN)) {
      return name;
    } else {
      return this.quotingDelimiter + name + this.quotingDelimiter;
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy