Fix and fine-tune suppress_nulls.
[squelch.git] / src / main / java / net / jaekl / squelch / db / DbDriver.java
1 package net.jaekl.squelch.db;
2
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;
8
9 public abstract class DbDriver {
10         // Well-known setting names
11         public static final String SUPPRESS_NULLS = "suppress_nulls";
12         
13         private HashMap<String, Setting> m_settings;
14         
15         // Returns true iff. this DbDriver knows how to connect to the given JDBC URL
16         abstract public boolean handles(String jdbcUrl);
17         
18         // Execute line as a statement of this type
19         abstract String getJdbcDriverClassName();
20
21         DbDriver() {
22                 m_settings = new HashMap<String, Setting>();
23                 m_settings.put(SUPPRESS_NULLS, new Setting(SUPPRESS_NULLS, Boolean.class, Boolean.valueOf(false)));
24         }
25         
26         // -------------------
27         // Getters and setters
28         public Setting[] getSettings() 
29         {
30                 return m_settings.values().toArray(new Setting[m_settings.size()]);
31         }
32         
33         public boolean isSet(String name) 
34         {
35                 Setting setting = m_settings.get(name);
36                 if (null != setting) {
37                         return setting.getBoolean();
38                 }
39                 return false;
40         }
41         public void set(String name, Object value) 
42         {
43                 String lcName = name.toLowerCase(Locale.CANADA);
44                 Setting setting = m_settings.get(lcName);
45                 if (null != setting) {
46                         setting.set(value);
47                 }
48                 else {
49                         throw new IllegalArgumentException("Setting \"" + name + "\" not found.");
50                 }
51         }
52                 
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 
55         {
56                 Class.forName(getJdbcDriverClassName());
57                 return DriverManager.getConnection(jdbcUrl, userName, password);
58         }
59         
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) {
64                 if (null == input) {
65                         return "";      // Convert nulls to empty strings, so that we can safely use .equals() on the result
66                 }
67                 return input.toUpperCase(Locale.CANADA);
68         }
69 }