Skip to content

Commit 85fecc9

Browse files
committed
fj-bom version set to 1.6.5
- DBUtils.indentifyDB() now recognizes h2 (600) and hsqldb (700) databases. - IdGenerator for h2 is mapped to Postgres by default
1 parent a057ff4 commit 85fecc9

File tree

8 files changed

+106
-15
lines changed

8 files changed

+106
-15
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- fj-bom version set to 1.6.5
13+
- DBUtils.indentifyDB() now recognizes h2 (600) and hsqldb (700) databases.
14+
- IdGenerator for h2 is mapped to Postgres by default
15+
1016
## [8.5.5] - 2024-04-06
1117

1218
### Added

fj-core/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@
9292
<scope>test</scope>
9393
</dependency>
9494

95+
<dependency>
96+
<groupId>com.h2database</groupId>
97+
<artifactId>h2</artifactId>
98+
<scope>test</scope>
99+
</dependency>
100+
95101
<dependency>
96102
<groupId>com.github.h-thurow</groupId>
97103
<artifactId>simple-jndi</artifactId>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.fugerit.java.core.db.dao.idgen;
2+
3+
public class HsqldbSeqIdGenerator extends BasicSeqIdGenerator {
4+
5+
public static String createSequenceQuery( String seqName) {
6+
return "call NEXT VALUE FOR "+seqName;
7+
}
8+
9+
/* (non-Javadoc)
10+
* @see org.morozko.java.mod.db.dao.idgen.BasicSeqIdGenerator#createSequenceQuery()
11+
*/
12+
@Override
13+
protected String createSequenceQuery() {
14+
return createSequenceQuery( this.getSequenceName() );
15+
}
16+
17+
}

fj-core/src/main/java/org/fugerit/java/core/db/helpers/DbUtils.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ private DbUtils() {}
2525
public static final int DB_MYSQL = 200;
2626

2727
public static final int DB_ORACLE = 300;
28-
29-
public static final int DB_DB2 = 400;
30-
28+
3129
public static final int DB_SQLSERVER = 500;
32-
30+
31+
public static final int DB_H2 = 600;
32+
33+
public static final int DB_HSQLDB = 700;
34+
3335
public static void close( Connection conn ) throws DAOException {
3436
if ( conn != null ) {
3537
try {
@@ -43,30 +45,33 @@ public static void close( Connection conn ) throws DAOException {
4345
public static int indentifyDB( String productName ) {
4446
int dbType = DB_UNKNOWN;
4547
String name = productName.toLowerCase();
46-
log.info( "Configuring Database specific logic, driver : {}", name );
48+
log.info( "Configuring Database specific logic, productName : {}", name );
4749
if ( name.indexOf( "postgres" ) != -1 ) {
4850
dbType = DB_POSTGRESQL;
49-
log.info( "IdGenerator configured for : POSTGRESQL ({})", dbType );
51+
log.info( "db type : POSTGRESQL ({})", dbType );
5052
} else if ( name.indexOf( "oracle" ) != -1 ) {
5153
dbType = DB_ORACLE;
52-
log.info( "IdGenerator configured for : ORACLE ({})", dbType );
54+
log.info( "db type : ORACLE ({})", dbType );
5355
} else if ( name.indexOf( "sqlserver" ) != -1 ) {
5456
dbType = DB_SQLSERVER;
55-
log.info( "IdGenerator configured for : SQLSERVER ({})", dbType );
57+
log.info( "db type : SQLSERVER ({})", dbType );
58+
} else if ( name.indexOf( "h2" ) != -1 ) {
59+
dbType = DB_H2;
60+
log.info( "db type : H2 ({})", dbType );
5661
} else if ( name.indexOf( "mysql" ) != -1 ) {
5762
dbType = DB_MYSQL;
58-
log.info( "IdGenerator configured for : MYSQL ({})", dbType );
63+
log.info( "db type : MYSQL ({})", dbType );
5964
} else if ( name.indexOf( "hsql" ) != -1 ) {
60-
dbType = DB_POSTGRESQL;
61-
log.info( "IdGenerator configured for : POSTGRESQL ({}) was ({})", dbType, name );
65+
dbType = DB_HSQLDB;
66+
log.info( "db type : HSQLDB ({}) was ({})", dbType, name );
6267
} else {
6368
log.info( "Unknown db type ({})", dbType );
6469
}
6570
return dbType;
6671
}
6772

6873
public static int indentifyDB( Connection conn ) throws SQLException {
69-
String name = conn.getMetaData().getDriverName().toLowerCase();
74+
String name = conn.getMetaData().getDatabaseProductName().toLowerCase();
7075
return indentifyDB( name );
7176
}
7277

fj-core/src/main/java/org/fugerit/java/core/lang/ex/ExConverUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class ExConverUtils {
44

55
private ExConverUtils() {}
66

7-
public static final String DEFAULT_CAUSE_MESSAGE = "DocException cause is";
7+
public static final String DEFAULT_CAUSE_MESSAGE = "Exception cause is";
88

99
public static String defaultMethodMessage( String method ) {
1010
return "Exception during "+method;
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
100=org.fugerit.java.core.db.dao.idgen.PostgresqlSeqIdGenerator
22
200=org.fugerit.java.core.db.dao.idgen.MysqlSeqIdGenerator
33
300=org.fugerit.java.core.db.dao.idgen.OracleSeqIdGenerator
4-
#400=org.fugerit.java.core.db.dao.idgen.
54
500=org.fugerit.java.core.db.dao.idgen.SqlServerSeqIdGenerator
5+
600=org.fugerit.java.core.db.dao.idgen.PostgresqlSeqIdGenerator
6+
700=org.fugerit.java.core.db.dao.idgen.HsqldbSeqIdGenerator
67
900=org.fugerit.java.core.db.dao.idgen.PostgresqlSeqIdGenerator
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package test.org.fugerit.java.core.db.helpers;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.fugerit.java.core.db.connect.ConnectionFactory;
5+
import org.fugerit.java.core.db.connect.ConnectionFactoryImpl;
6+
import org.fugerit.java.core.db.dao.DAOUtils;
7+
import org.fugerit.java.core.db.dao.DAOUtilsNG;
8+
import org.fugerit.java.core.db.dao.OpDAO;
9+
import org.fugerit.java.core.db.dao.idgen.BasicSeqIdGenerator;
10+
import org.fugerit.java.core.db.dao.idgen.IdGeneratorFacade;
11+
import org.fugerit.java.core.db.helpers.DAOID;
12+
import org.fugerit.java.core.db.helpers.DbUtils;
13+
import org.fugerit.java.core.function.SafeFunction;
14+
import org.junit.Assert;
15+
import org.junit.Test;
16+
import test.org.fugerit.java.BasicTest;
17+
18+
import java.sql.Connection;
19+
import java.sql.DriverManager;
20+
21+
@Slf4j
22+
public class TestDBUtils extends BasicTest {
23+
24+
private int testWorker( ConnectionFactory cf ) {
25+
return SafeFunction.get( () -> {
26+
try ( Connection conn = cf.getConnection() ) {
27+
DAOUtilsNG.execute( conn, OpDAO.newExecuteOp( "CREATE SEQUENCE IF NOT EXISTS test_seq AS INTEGER START WITH 10" ) );
28+
int res = DbUtils.indentifyDB( conn );
29+
BasicSeqIdGenerator idGenerator = IdGeneratorFacade.sequenceGenerator( conn, "test_seq" );
30+
DAOID id = idGenerator.generateId( conn );
31+
log.info( "generated id : {}", id );
32+
Assert.assertNotNull( id );
33+
return res;
34+
}
35+
} );
36+
}
37+
38+
@Test
39+
public void testH2() {
40+
SafeFunction.apply( () -> {
41+
ConnectionFactory cf = ConnectionFactoryImpl.newInstance( "org.h2.Driver",
42+
"jdbc:h2:mem:default", "user_h2", "pass_h2" );
43+
Assert.assertEquals( DbUtils.DB_H2, this.testWorker( cf ) );
44+
} );
45+
}
46+
47+
@Test
48+
public void testHsqldb() {
49+
SafeFunction.apply( () -> {
50+
ConnectionFactory cf = ConnectionFactoryImpl.newInstance( "org.hsqldb.jdbc.JDBCDriver",
51+
"jdbc:hsqldb:mem:dbutils_test", "user_hsqldb", "pass_hsqldb" );
52+
Assert.assertEquals( DbUtils.DB_HSQLDB, this.testWorker( cf ) );
53+
} );
54+
}
55+
56+
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parent>
88
<groupId>org.fugerit.java</groupId>
99
<artifactId>fj-bom</artifactId>
10-
<version>1.6.4</version>
10+
<version>1.6.5</version>
1111
<relativePath></relativePath>
1212
</parent>
1313

0 commit comments

Comments
 (0)