View Javadoc
1   /*
2    * JDBCHelper.java
3    *
4    * Created on 18 May 2007, 14:12
5    *
6    * To change this template, choose Tools | Template Manager
7    * and open the template in the editor.
8    */
9   package com.wakaleo.schemaspy.util;
10  
11  /**
12   * A utilities class for the SchemaSpy plugin. Tries to guess the right
13   * SchemaSpy database type configuration value using a JDBC URL.
14   *
15   * @author john
16   */
17  public final class JDBCHelper {
18  
19      /** Creates a new instance of JDBCHelper. */
20      private JDBCHelper() {
21      }
22  
23      /**
24       * A map of JDBC prefixes and database types. Used to determine the database type based on a JDBC
25       * URL.
26       */
27      private static final String[][] DATABASE_TYPES_MAP = {
28          {"jdbc:derby:", "derby"},
29          {"jdbc:db2:", "db2"},
30          {"jdbc:firebirdsql:", "firebirdsql"},
31          {"jdbc:hsqldb:hsql:", "hsqldb"},
32          {"jdbc:informix-sqli:", "informix-sqli"},
33          {"jdbc:microsoft:sqlserver:", "mssql"},
34          {"jdbc:jtds:", "mssql-jtds"},
35          {"jdbc:mysql:", "mysql"},
36          {"jdbc:oracle:oci8:", "ora"},
37          {"jdbc:oracle:thin:", "orathin"},
38          {"jdbc:postgresql:", "pgsql"},
39          {"jdbc:sybase:Tds:", "sybase"}
40      };
41  
42      /**
43       * Find the matching SchemaSpy database type from a JDBC URL. If none match, {@code null} is
44       * returned.
45       *
46       * @param jdbcUrl a valid JDBC url for the target database
47       * @return the type of the database for this JDBC URL
48       */
49      public static String extractDatabaseType(final String jdbcUrl) {
50          String result = null;
51          for (String[] databaseTypeEntry : DATABASE_TYPES_MAP) {
52              String jdbcPrefix = databaseTypeEntry[0];
53              if (jdbcUrl.startsWith(jdbcPrefix)) {
54                  result = databaseTypeEntry[1];
55              }
56          }
57          return result;
58      }
59  }