Fix and fine-tune suppress_nulls.
[squelch.git] / src / main / java / net / jaekl / squelch / db / DbDriver.java
index 5e73885033e4d06ae2b17c13ec27c7d7597f57f3..cf116de5b8f3ba39c9c73cfb6553ed7d6a320592 100644 (file)
@@ -3,18 +3,67 @@ package net.jaekl.squelch.db;
 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);
+       }
 }