1 package net.jaekl.squelch.db;
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.SQLException;
6 import java.util.HashMap;
7 import java.util.Locale;
9 public abstract class DbDriver {
10 // Well-known setting names
11 public static final String SUPPRESS_NULLS = "suppress_nulls";
13 private HashMap<String, Setting> m_settings;
15 // Returns true iff. this DbDriver knows how to connect to the given JDBC URL
16 abstract public boolean handles(String jdbcUrl);
18 // Execute line as a statement of this type
19 abstract String getJdbcDriverClassName();
22 m_settings = new HashMap<String, Setting>();
23 m_settings.put(SUPPRESS_NULLS, new Setting(SUPPRESS_NULLS, Boolean.class, Boolean.valueOf(false)));
26 // -------------------
27 // Getters and setters
28 public Setting[] getSettings()
30 return m_settings.values().toArray(new Setting[m_settings.size()]);
33 public boolean isSet(String name)
35 Setting setting = m_settings.get(name);
36 if (null != setting) {
37 return setting.getBoolean();
41 public void set(String name, Object value)
43 String lcName = name.toLowerCase(Locale.CANADA);
44 Setting setting = m_settings.get(lcName);
45 if (null != setting) {
49 throw new IllegalArgumentException("Setting \"" + name + "\" not found.");
53 // Open a new Connection to the database. Note that the caller must close() this at some point.
54 public Connection connect(String jdbcUrl, String userName, String password) throws ClassNotFoundException, SQLException
56 Class.forName(getJdbcDriverClassName());
57 return DriverManager.getConnection(jdbcUrl, userName, password);
60 // If this database uses case-insensitive collation, then return an upper-cased version of the passed string.
61 // Otherwise (if this database is case-sensitive), return the string unchanged.
62 // Note that the default implementation assumes a case-insensitive DB.
63 public String adjustCase(String input) {
65 return ""; // Convert nulls to empty strings, so that we can safely use .equals() on the result
67 return input.toUpperCase(Locale.CANADA);