@ -17,11 +17,8 @@
package org.springframework.boot.jdbc ;
import java.sql.Connection ;
import java.sql.DatabaseMetaData ;
import java.sql.SQLException ;
import java.util.Locale ;
import java.util.function.Predicate ;
import java.util.stream.Stream ;
import javax.sql.DataSource ;
@ -46,25 +43,24 @@ public enum EmbeddedDatabaseConnection {
/ * *
* No Connection .
* /
NONE ( null , null , null , ( url ) - > false ),
NONE ( null , null , null ),
/ * *
* H2 Database Connection .
* /
H2 ( EmbeddedDatabaseType . H2 , DatabaseDriver . H2 . getDriverClassName ( ) ,
"jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE" , ( url ) - > url . contains ( ":h2:mem" ) ),
"jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE" ),
/ * *
* Derby Database Connection .
* /
DERBY ( EmbeddedDatabaseType . DERBY , DatabaseDriver . DERBY . getDriverClassName ( ) , "jdbc:derby:memory:%s;create=true" ,
( url ) - > true ) ,
DERBY ( EmbeddedDatabaseType . DERBY , DatabaseDriver . DERBY . getDriverClassName ( ) , "jdbc:derby:memory:%s;create=true" ) ,
/ * *
* HSQL Database Connection .
* /
HSQL ( EmbeddedDatabaseType . HSQL , DatabaseDriver . HSQLDB . getDriverClassName ( ) , "org.hsqldb.jdbcDriver" ,
"jdbc:hsqldb:mem:%s" , ( url ) - > url . contains ( ":hsqldb:mem:" ) );
"jdbc:hsqldb:mem:%s" );
private final EmbeddedDatabaseType type ;
@ -74,20 +70,15 @@ public enum EmbeddedDatabaseConnection {
private final String url ;
private final Predicate < String > embeddedUrl ;
EmbeddedDatabaseConnection ( EmbeddedDatabaseType type , String driverClass , String url ,
Predicate < String > embeddedUrl ) {
this ( type , driverClass , null , url , embeddedUrl ) ;
EmbeddedDatabaseConnection ( EmbeddedDatabaseType type , String driverClass , String url ) {
this ( type , driverClass , null , url ) ;
}
EmbeddedDatabaseConnection ( EmbeddedDatabaseType type , String driverClass , String fallbackDriverClass , String url ,
Predicate < String > embeddedUrl ) {
EmbeddedDatabaseConnection ( EmbeddedDatabaseType type , String driverClass , String fallbackDriverClass , String url ) {
this . type = type ;
this . driverClass = driverClass ;
this . alternativeDriverClass = fallbackDriverClass ;
this . url = url ;
this . embeddedUrl = embeddedUrl ;
}
/ * *
@ -116,48 +107,19 @@ public enum EmbeddedDatabaseConnection {
return ( this . url ! = null ) ? String . format ( this . url , databaseName ) : null ;
}
boolean isEmbeddedUrl ( String url ) {
return this . embeddedUrl . test ( url ) ;
}
boolean isDriverCompatible ( String driverClass ) {
return ( driverClass ! = null
& & ( driverClass . equals ( this . driverClass ) | | driverClass . equals ( this . alternativeDriverClass ) ) ) ;
}
/ * *
* Convenience method to determine if a given driver class name represents an embedded
* database type .
* @param driverClass the driver class
* @return true if the driver class is one of the embedded types
* @deprecated since 2.3 .5 in favor of { @link # isEmbedded ( String , String ) }
* /
@Deprecated
public static boolean isEmbedded ( String driverClass ) {
return isEmbedded ( driverClass , null ) ;
}
/ * *
* Convenience method to determine if a given driver class name and url represent an
* embedded database type .
* @param driverClass the driver class
* @param url the jdbc url ( can be { @code null ) }
* @return true if the driver class and url refer to an embedded database
* /
public static boolean isEmbedded ( String driverClass , String url ) {
if ( driverClass = = null ) {
return false ;
}
EmbeddedDatabaseConnection connection = getEmbeddedDatabaseConnection ( driverClass ) ;
if ( connection = = NONE ) {
return false ;
}
return ( url = = null | | connection . isEmbeddedUrl ( url ) ) ;
return driverClass ! = null
& & ( matches ( HSQL , driverClass ) | | matches ( H2 , driverClass ) | | matches ( DERBY , driverClass ) ) ;
}
private static EmbeddedDatabaseConnection getEmbeddedDatabaseConnection ( String driverClass ) {
return Stream . of ( H2 , HSQL , DERBY ) . filter ( ( connection ) - > connection . isDriverCompatible ( driverClass ) ) . findFirst ( )
. orElse ( NONE ) ;
private static boolean matches ( EmbeddedDatabaseConnection candidate , String driverClass ) {
return driverClass . equals ( candidate . driverClass ) | | driverClass . equals ( candidate . alternativeDriverClass ) ;
}
/ * *
@ -198,8 +160,7 @@ public enum EmbeddedDatabaseConnection {
@Override
public Boolean doInConnection ( Connection connection ) throws SQLException , DataAccessException {
DatabaseMetaData metaData = connection . getMetaData ( ) ;
String productName = metaData . getDatabaseProductName ( ) ;
String productName = connection . getMetaData ( ) . getDatabaseProductName ( ) ;
if ( productName = = null ) {
return false ;
}
@ -207,8 +168,7 @@ public enum EmbeddedDatabaseConnection {
EmbeddedDatabaseConnection [ ] candidates = EmbeddedDatabaseConnection . values ( ) ;
for ( EmbeddedDatabaseConnection candidate : candidates ) {
if ( candidate ! = NONE & & productName . contains ( candidate . name ( ) ) ) {
String url = metaData . getURL ( ) ;
return ( url = = null | | candidate . isEmbeddedUrl ( url ) ) ;
return true ;
}
}
return false ;