|
| 1 | +/* |
| 2 | + * Copyright 2012-2014 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.jdbc; |
| 18 | + |
| 19 | +import org.springframework.util.Assert; |
| 20 | +import org.springframework.util.StringUtils; |
| 21 | + |
| 22 | +/** |
| 23 | + * Enumeration of common database drivers. |
| 24 | + * |
| 25 | + * @author Phillip Webb |
| 26 | + * @author Maciej Walkowiak |
| 27 | + * @since 1.2.0 |
| 28 | + */ |
| 29 | +enum DatabaseDriver { |
| 30 | + |
| 31 | + /** |
| 32 | + * Unknown type. |
| 33 | + */ |
| 34 | + UNKNOWN(null), |
| 35 | + |
| 36 | + /** |
| 37 | + * Apache Derby. |
| 38 | + */ |
| 39 | + DERBY("org.apache.derby.jdbc.EmbeddedDriver"), |
| 40 | + |
| 41 | + /** |
| 42 | + * H2. |
| 43 | + */ |
| 44 | + H2("org.h2.Driver", "org.h2.jdbcx.JdbcDataSource"), |
| 45 | + |
| 46 | + /** |
| 47 | + * HyperSQL DataBase. |
| 48 | + */ |
| 49 | + HSQLDB("org.hsqldb.jdbc.JDBCDriver", "org.hsqldb.jdbc.pool.JDBCXADataSource"), |
| 50 | + |
| 51 | + /** |
| 52 | + * SQL Lite. |
| 53 | + */ |
| 54 | + SQLITE("org.sqlite.JDBC"), |
| 55 | + |
| 56 | + /** |
| 57 | + * MySQL. |
| 58 | + */ |
| 59 | + MYSQL("com.mysql.jdbc.Driver", "org.mysql.jdbc.MySQLDataSource"), |
| 60 | + |
| 61 | + /** |
| 62 | + * Maria DB. |
| 63 | + */ |
| 64 | + MARIADB("org.mariadb.jdbc.Driver", "org.mariadb.jdbc.MySQLDataSource"), |
| 65 | + |
| 66 | + /** |
| 67 | + * Google App Engine. |
| 68 | + */ |
| 69 | + GOOGLE("com.google.appengine.api.rdbms.AppEngineDriver"), |
| 70 | + |
| 71 | + /** |
| 72 | + * Oracle |
| 73 | + */ |
| 74 | + ORACLE("oracle.jdbc.OracleDriver", "oracle.jdbc.xa.OracleXADataSource"), |
| 75 | + |
| 76 | + /** |
| 77 | + * Postres |
| 78 | + */ |
| 79 | + POSTGRESQL("org.postgresql.Driver", "org.postgresql.xa.PGXADataSource"), |
| 80 | + |
| 81 | + /** |
| 82 | + * JTDS |
| 83 | + */ |
| 84 | + JTDS("net.sourceforge.jtds.jdbc.Driver"), |
| 85 | + |
| 86 | + /** |
| 87 | + * SQL Server |
| 88 | + */ |
| 89 | + SQLSERVER("com.microsoft.sqlserver.jdbc.SQLServerDriver"); |
| 90 | + |
| 91 | + private final String driverClassName; |
| 92 | + |
| 93 | + private final String xaDataSourceClassName; |
| 94 | + |
| 95 | + private DatabaseDriver(String driverClassName) { |
| 96 | + this(driverClassName, null); |
| 97 | + } |
| 98 | + |
| 99 | + private DatabaseDriver(String driverClassName, String xaDataSourceClassName) { |
| 100 | + this.driverClassName = driverClassName; |
| 101 | + this.xaDataSourceClassName = xaDataSourceClassName; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @return the driverClassName or {@code null} |
| 106 | + */ |
| 107 | + public String getDriverClassName() { |
| 108 | + return this.driverClassName; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @return the xaDataSourceClassName or {@code null} |
| 113 | + */ |
| 114 | + public String getXaDataSourceClassName() { |
| 115 | + return this.xaDataSourceClassName; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Find a {@link DatabaseDriver} for the given URL. |
| 120 | + * @param url JDBC URL |
| 121 | + * @return driver class name or {@link #UNKNOWN} if not found |
| 122 | + */ |
| 123 | + public static DatabaseDriver fromJdbcUrl(String url) { |
| 124 | + if (StringUtils.hasLength(url)) { |
| 125 | + Assert.isTrue(url.startsWith("jdbc"), "URL must start with 'jdbc'"); |
| 126 | + String urlWithoutPrefix = url.substring("jdbc".length()).toLowerCase(); |
| 127 | + for (DatabaseDriver driver : values()) { |
| 128 | + String prefix = ":" + driver.name().toLowerCase() + ":"; |
| 129 | + if (driver != UNKNOWN && urlWithoutPrefix.startsWith(prefix)) { |
| 130 | + return driver; |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + return UNKNOWN; |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments