72472de3c2a0992825c535f6183ccb41afb0fc50
[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.Locale;
7
8 public abstract class DbDriver {
9         private boolean m_suppressNulls = false;
10         
11         // Returns true iff. this DbDriver knows how to connect to the given JDBC URL
12         abstract public boolean handles(String jdbcUrl);
13         
14         // Execute line as a statement of this type
15         abstract String getJdbcDriverClassName();
16
17         // -------------------
18         // Getters and setters
19         
20         public boolean isSuppressNulls() { return m_suppressNulls; }
21         public void setSuppressNulls(boolean value) { m_suppressNulls = value; }
22         
23         
24         // Open a new Connection to the database.  Note that the caller must close() this at some point.
25         public Connection connect(String jdbcUrl, String userName, String password) throws ClassNotFoundException, SQLException 
26         {
27                 Class.forName(getJdbcDriverClassName());
28                 return DriverManager.getConnection(jdbcUrl, userName, password);
29         }
30         
31         // If this database uses case-insensitive collation, then return an upper-cased version of the passed string.
32         // Otherwise (if this database is case-sensitive), return the string unchanged.
33         // Note that the default implementation assumes a case-insensitive DB.
34         public String adjustCase(String input) {
35                 if (null == input) {
36                         return "";      // Convert nulls to empty strings, so that we can safely use .equals() on the result
37                 }
38                 return input.toUpperCase(Locale.CANADA);
39         }
40 }