-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mateusz Witkowski
committed
Nov 22, 2024
1 parent
3fd8816
commit 4bb1b81
Showing
3 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...ring/src/main/java/dev/logchange/hofund/connection/spring/datasource/h2/H2Connection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package dev.logchange.hofund.connection.spring.datasource.h2; | ||
|
||
import dev.logchange.hofund.connection.spring.datasource.DatasourceConnection; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.DatabaseMetaData; | ||
import java.sql.SQLException; | ||
import java.util.Locale; | ||
|
||
@Slf4j | ||
public class H2Connection extends DatasourceConnection { | ||
|
||
private static final String TEST_QUERY = "SELECT 1"; | ||
|
||
private String target; | ||
private String url; | ||
private String dbVendor; | ||
|
||
public H2Connection(DatabaseMetaData metaData, DataSource dataSource) { | ||
super(dataSource, TEST_QUERY); | ||
try { | ||
this.url = metaData.getURL(); | ||
int slashIndex = url.lastIndexOf('/'); | ||
int to = url.length(); | ||
if (url.lastIndexOf("?") != -1) { | ||
to = url.lastIndexOf("?"); | ||
} | ||
this.target = url.substring(slashIndex + 1, to).toLowerCase(Locale.ROOT); | ||
this.dbVendor = metaData.getDatabaseProductName(); | ||
log.info("H2 info: target: {}, url: {}, dbVendor: {}", target, url, dbVendor); | ||
} catch (SQLException e) { | ||
log.warn("Error getting db information", e); | ||
this.target = "ERROR"; | ||
this.url = "ERROR"; | ||
this.dbVendor = "ERROR"; | ||
} | ||
} | ||
|
||
@Override | ||
protected String getTarget() { | ||
return target; | ||
} | ||
|
||
@Override | ||
protected String getUrl() { | ||
return url; | ||
} | ||
|
||
@Override | ||
protected String getDbVendor() { | ||
return dbVendor; | ||
} | ||
|
||
} |