import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.Locale;
public abstract class DbDriver {
+ // Well-known setting names
+ public static final String SUPPRESS_NULLS = "suppress_nulls";
+
+ private HashMap<String, Setting> m_settings;
+
// Returns true iff. this DbDriver knows how to connect to the given JDBC URL
abstract public boolean handles(String jdbcUrl);
// Execute line as a statement of this type
abstract String getJdbcDriverClassName();
+
+ DbDriver() {
+ m_settings = new HashMap<String, Setting>();
+ m_settings.put(SUPPRESS_NULLS, new Setting(SUPPRESS_NULLS, Boolean.class, Boolean.valueOf(false)));
+ }
+
+ // -------------------
+ // Getters and setters
+ public Setting[] getSettings()
+ {
+ return m_settings.values().toArray(new Setting[m_settings.size()]);
+ }
+ public boolean isSet(String name)
+ {
+ Setting setting = m_settings.get(name);
+ if (null != setting) {
+ return setting.getBoolean();
+ }
+ return false;
+ }
+ public void set(String name, Object value)
+ {
+ String lcName = name.toLowerCase(Locale.CANADA);
+ Setting setting = m_settings.get(lcName);
+ if (null != setting) {
+ setting.set(value);
+ }
+ else {
+ throw new IllegalArgumentException("Setting \"" + name + "\" not found.");
+ }
+ }
+
// Open a new Connection to the database. Note that the caller must close() this at some point.
public Connection connect(String jdbcUrl, String userName, String password) throws ClassNotFoundException, SQLException
{
Class.forName(getJdbcDriverClassName());
return DriverManager.getConnection(jdbcUrl, userName, password);
}
+
+ // If this database uses case-insensitive collation, then return an upper-cased version of the passed string.
+ // Otherwise (if this database is case-sensitive), return the string unchanged.
+ // Note that the default implementation assumes a case-insensitive DB.
+ public String adjustCase(String input) {
+ if (null == input) {
+ return ""; // Convert nulls to empty strings, so that we can safely use .equals() on the result
+ }
+ return input.toUpperCase(Locale.CANADA);
+ }
}