Skip to content

Commit 9f4cc17

Browse files
committed
Code optimization
1 parent 11880be commit 9f4cc17

File tree

9 files changed

+44
-36
lines changed

9 files changed

+44
-36
lines changed

fj-daogen-sample/src/main/java/org/fugerit/java/daogen/sample/helper/ServiceProviderHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import org.fugerit.java.core.db.daogen.CloseableDAOContext;
66
import org.fugerit.java.core.db.daogen.CloseableDAOContextSC;
77
import org.fugerit.java.core.db.daogen.SimpleServiceProvider;
8+
import org.fugerit.java.daogen.sample.def.facade.FugeritLogicFacadeHelper;
89
import org.fugerit.java.daogen.sample.impl.facade.data.FugeritDataLogicFacade;
9-
import org.fugerit.java.daogen.sample.impl.facade.data.FugeritDataLogicFacadeHelper;
1010
import org.fugerit.java.test.db.helper.MemDBHelper;
1111

1212
public class ServiceProviderHelper<T> extends SimpleServiceProvider<T> {
@@ -24,7 +24,7 @@ protected CloseableDAOContext newDefaultContext() throws DAOException {
2424
* The object asking for the context is responsible for closing it.
2525
*/
2626
context = new CloseableDAOContextSC( MemDBHelper.newConnection() ); // NOSONAR
27-
context.setAttribute( FugeritDataLogicFacadeHelper.ATT_NAME, new FugeritDataLogicFacade() );
27+
context.setAttribute( FugeritLogicFacadeHelper.ATT_NAME, new FugeritDataLogicFacade() );
2828
return context;
2929
} catch (Exception e) {
3030
if ( context != null ) {

fj-daogen-sample/src/main/java/org/fugerit/java/daogen/sample/impl/struct/ObjAddress.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public ObjAddress() {
4444

4545
@Override
4646
public Map<String, Class<?>> newTypeMapper() throws SQLException {
47-
Map<String, Class<?>> map = new HashMap<String, Class<?>>();
47+
Map<String, Class<?>> map = new HashMap<>();
4848
map.put( SQL_TYPE_NAME, ObjAddress.class );
4949
return map;
5050
}

fj-daogen-sample/src/main/java/org/fugerit/java/daogen/sample/impl/struct/ObjLogData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public ObjLogData() {
4444

4545
@Override
4646
public Map<String, Class<?>> newTypeMapper() throws SQLException {
47-
Map<String, Class<?>> map = new HashMap<String, Class<?>>();
47+
Map<String, Class<?>> map = new HashMap<>();
4848
map.put( SQL_TYPE_NAME, ObjLogData.class );
4949
return map;
5050
}

fj-daogen-sample/src/main/java/org/fugerit/java/daogen/sample/impl/struct/ObjTestTwoFieldKey.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public ObjTestTwoFieldKey() {
4444

4545
@Override
4646
public Map<String, Class<?>> newTypeMapper() throws SQLException {
47-
Map<String, Class<?>> map = new HashMap<String, Class<?>>();
47+
Map<String, Class<?>> map = new HashMap<>();
4848
map.put( SQL_TYPE_NAME, ObjTestTwoFieldKey.class );
4949
return map;
5050
}

fj-daogen-sample/src/main/java/org/fugerit/java/daogen/sample/impl/struct/ObjUpload.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public ObjUpload() {
4444

4545
@Override
4646
public Map<String, Class<?>> newTypeMapper() throws SQLException {
47-
Map<String, Class<?>> map = new HashMap<String, Class<?>>();
47+
Map<String, Class<?>> map = new HashMap<>();
4848
map.put( SQL_TYPE_NAME, ObjUpload.class );
4949
return map;
5050
}
@@ -92,9 +92,7 @@ public void setupLobs( java.sql.Connection conn ) throws SQLException {
9292

9393
public static ObjUpload wrap( ModelUpload model, java.sql.Connection conn ) throws SQLException {
9494
ObjUpload res = wrap( model );
95-
if ( res != null ) {
9695
res.setupLobs( conn );
97-
}
9896
return res;
9997
}
10098

fj-daogen-sample/src/main/java/org/fugerit/java/daogen/sample/impl/struct/ObjUser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public ObjUser() {
4444

4545
@Override
4646
public Map<String, Class<?>> newTypeMapper() throws SQLException {
47-
Map<String, Class<?>> map = new HashMap<String, Class<?>>();
47+
Map<String, Class<?>> map = new HashMap<>();
4848
map.put( SQL_TYPE_NAME, ObjUser.class );
4949
return map;
5050
}

fj-daogen-sample/src/main/java/org/fugerit/java/daogen/sample/impl/struct/ObjUserData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public ObjUserData() {
4444

4545
@Override
4646
public Map<String, Class<?>> newTypeMapper() throws SQLException {
47-
Map<String, Class<?>> map = new HashMap<String, Class<?>>();
47+
Map<String, Class<?>> map = new HashMap<>();
4848
map.put( SQL_TYPE_NAME, ObjUserData.class );
4949
return map;
5050
}

fj-daogen-sample/src/main/java/org/fugerit/java/test/db/helper/MemDBHelper.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@
1010
import java.sql.Statement;
1111
import java.util.Properties;
1212

13+
import org.fugerit.java.core.db.dao.DAORuntimeException;
14+
1315
public class MemDBHelper {
1416

17+
private MemDBHelper() {}
18+
1519
public static final String DEFAULT_DB_CONN_PATH = "test/memdb/base-db-conn.properties";
1620
public static final String DEFAULT_DB_INIT_PATH = "test/memdb/base_db_init.sql";
1721

@@ -23,28 +27,32 @@ public class MemDBHelper {
2327

2428
private static Properties cf;
2529

26-
private static Connection newConnection( Properties props ) throws Exception {
27-
Class.forName( props.getProperty( DRV ) );
28-
return DriverManager.getConnection( props.getProperty( URL ), props.getProperty( USR ), props.getProperty( PWD ) );
30+
private static Connection newConnection( Properties props ) {
31+
return DAORuntimeException.get( () -> {
32+
Class.forName( props.getProperty( DRV ) );
33+
return DriverManager.getConnection( props.getProperty( URL ), props.getProperty( USR ), props.getProperty( PWD ) );
34+
} );
2935
}
3036

31-
public static void init() throws Exception
37+
public static void init()
3238
{
3339
if ( cf == null ) {
34-
try ( InputStream is = MemDBHelper.class.getClassLoader().getResourceAsStream( DEFAULT_DB_CONN_PATH ) ) {
35-
Properties props = new Properties();
36-
props.load( is );
37-
try ( Connection conn = newConnection( props ) ) {
38-
try ( SQLScriptReader reader = new SQLScriptReader( MemDBHelper.class.getClassLoader().getResourceAsStream( DEFAULT_DB_INIT_PATH ) ) ) {
39-
executeAll(reader, conn);
40-
cf = props;
41-
}
40+
DAORuntimeException.apply( () -> {
41+
try ( InputStream is = MemDBHelper.class.getClassLoader().getResourceAsStream( DEFAULT_DB_CONN_PATH ) ) {
42+
Properties props = new Properties();
43+
props.load( is );
44+
try ( Connection conn = newConnection( props ) ) {
45+
try ( SQLScriptReader reader = new SQLScriptReader( MemDBHelper.class.getClassLoader().getResourceAsStream( DEFAULT_DB_INIT_PATH ) ) ) {
46+
executeAll(reader, conn);
47+
cf = props;
48+
}
49+
}
4250
}
43-
}
51+
});
4452
}
4553
}
4654

47-
public static Connection newConnection() throws Exception {
55+
public static Connection newConnection() {
4856
init();
4957
return newConnection( cf );
5058
}

fj-daogen-sample/src/main/resources/test/memdb/base_db_init.sql

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
SET DATABASE SQL SYNTAX PGS TRUE;
1+
SET DATABASE SQL SYNTAX ORA TRUE;
22

33
CREATE SCHEMA fugerit;
44

55
CREATE TABLE fugerit.user (
66
id bigint NOT NULL,
7-
username VARCHAR(128) NOT NULL,
8-
password VARCHAR(128) NOT NULL,
7+
username VARCHAR2(128) NOT NULL,
8+
password VARCHAR2(128) NOT NULL,
99
last_login TIMESTAMP,
1010
date_insert TIMESTAMP,
1111
date_update TIMESTAMP,
@@ -26,7 +26,7 @@ CREATE TABLE fugerit.address (
2626
id_user bigint NOT NULL,
2727
date_insert TIMESTAMP,
2828
date_update TIMESTAMP,
29-
info VARCHAR(256) NOT NULL
29+
info VARCHAR2(256) NOT NULL
3030

3131
);
3232
ALTER TABLE fugerit.address ADD CONSTRAINT addresses_pk PRIMARY KEY ( id );
@@ -50,23 +50,25 @@ COMMENT ON TABLE fugerit.upload IS 'Contains upload blob';
5050
CREATE TABLE fugerit.log_data (
5151
id bigint NOT NULL,
5252
log_time TIMESTAMP,
53-
info VARCHAR(128) NOT NULL
53+
info VARCHAR2(128) NOT NULL
5454
);
5555

5656
CREATE TABLE fugerit.test_two_field_key (
5757
id_one bigint NOT NULL,
5858
id_two bigint NOT NULL,
59-
info VARCHAR(128) NOT NULL
59+
info VARCHAR2(128) NOT NULL
6060
);
6161
ALTER TABLE fugerit.test_two_field_key ADD CONSTRAINT test_two_field_key_pk PRIMARY KEY ( id_one, id_two );
6262

6363
CREATE SEQUENCE seq_test START WITH 1;
6464

65-
INSERT INTO fugerit.user ( id, username, password, last_login, state ) VALUES ( nextval('seq_test'), 'user1', 'a3f8h5h4h3n3n1n9', sysdate, 1 );
66-
INSERT INTO fugerit.user ( id, username, password, last_login, state ) VALUES ( nextval('seq_test'), 'user2', '5h4h3n3n1n9a3f8h', sysdate, 0 );
65+
INSERT INTO fugerit.user ( id, username, password, last_login, state ) VALUES ( seq_test.nextval, 'user1', 'a3f8h5h4h3n3n1n9', sysdate, 1 );
66+
INSERT INTO fugerit.user ( id, username, password, last_login, state ) VALUES ( seq_test.nextval, 'user2', '5h4h3n3n1n9a3f8h', sysdate, 0 );
67+
68+
INSERT INTO fugerit.address ( id, id_user, info ) VALUES ( seq_test.nextval, 1, 'test address 01' );
69+
INSERT INTO fugerit.address ( id, id_user, info ) VALUES ( seq_test.nextval, 2, 'test address 02' );
70+
INSERT INTO fugerit.address ( id, id_user, info ) VALUES ( seq_test.nextval, 1, 'test address 03' );
6771

68-
INSERT INTO fugerit.address ( id, id_user, info ) VALUES ( nextval('seq_test'), 1, 'test address 01' );
69-
INSERT INTO fugerit.address ( id, id_user, info ) VALUES ( nextval('seq_test'), 2, 'test address 02' );
70-
INSERT INTO fugerit.address ( id, id_user, info ) VALUES ( nextval('seq_test'), 1, 'test address 03' );
72+
INSERT INTO fugerit.test_two_field_key ( id_one, id_two, info ) VALUES ( seq_test.nextval, seq_test.nextval, 'test info 01' );
7173

72-
INSERT INTO fugerit.test_two_field_key ( id_one, id_two, info ) VALUES ( nextval('seq_test'), nextval('seq_test'), 'test info 01' );
74+
SET DATABASE SQL SYNTAX PGS TRUE;

0 commit comments

Comments
 (0)