org.nocrala.tools.database.sentinel.adapters.H2Adapter 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 H2Adapter implements DatabaseAdapter {
@SuppressWarnings("unused")
private DatabaseMetaData dm;
private String quotingDelimiter;
public H2Adapter(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().toUpperCase());
}
}
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;
}
}
}