Skip to content

Commit f1adb96

Browse files
committed
Fix initialization sql scripts
1 parent b0cf9e0 commit f1adb96

File tree

3 files changed

+28
-48
lines changed

3 files changed

+28
-48
lines changed

CHANGELOG.md

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

2020
- maven wrapper
2121

22+
### Fixed
23+
24+
- initialization sql scripts
25+
2226
## [1.3.2] - 2023-12-27
2327

2428
### Changed

src/main/java/org/fugerit/java/daogen/quickstart/config/QuickstartDBHelper.java

Lines changed: 16 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ public class QuickstartDBHelper {
2121
private QuickstartDBHelper() {}
2222

2323
private static final Logger logger = LoggerFactory.getLogger( QuickstartDBHelper.class );
24-
25-
public static final String DEFAULT_DB_CONN_PATH = "quickstart_db/quickstart-db-conn.properties";
26-
public static final String DEFAULT_DB_INIT_PATH = "quickstart_db/hsqldb";
24+
25+
private static final String DEFAULT_DB_CONN_PATH = "quickstart_db/quickstart-db-conn.properties";
26+
private static final String DEFAULT_DB_INIT_PATH = "quickstart_db/hsqldb";
27+
28+
private static final String[] SQL_INIT_SCRIPTS = { "100_db_setup.sql", "200_sample_db.sql", "900_examples.sql" };
2729

2830
private static final String DRV = "db-mode-dc-drv";
2931
private static final String URL = "db-mode-dc-url";
@@ -48,21 +50,19 @@ public static void init()
4850
Properties props = new Properties();
4951
props.load( is );
5052
try ( Connection conn = newConnection( props ) ) {
51-
cf = props;
52-
ResScanner scanner = new ResScanner();
53-
List<String> initFiles = scanner.getResourceFiles( DEFAULT_DB_INIT_PATH );
54-
for ( String current : initFiles ) {
55-
String res = DEFAULT_DB_INIT_PATH+"/"+current;
56-
logger.info( "Current : {}", res );
57-
try ( SQLScriptReader reader = new SQLScriptReader( QuickstartDBHelper.class.getClassLoader().getResourceAsStream( res ) ) ) {
58-
SQLScriptFacade.executeAll(reader, conn);
59-
cf = props;
60-
}
61-
}
62-
}
53+
for ( int k=0; k<SQL_INIT_SCRIPTS.length; k++ ) {
54+
String current = SQL_INIT_SCRIPTS[k];
55+
String res = DEFAULT_DB_INIT_PATH+"/"+current;
56+
logger.info( "Current : {}", res );
57+
try ( SQLScriptReader reader = new SQLScriptReader( QuickstartDBHelper.class.getClassLoader().getResourceAsStream( res ) ) ) {
58+
SQLScriptFacade.executeAll(reader, conn);
59+
60+
}
61+
}
62+
}
63+
cf = props;
6364
}
6465
} );
65-
6666
}
6767
}
6868

@@ -72,35 +72,3 @@ public static Connection newConnection() {
7272
}
7373

7474
}
75-
76-
class ResScanner {
77-
78-
public List<String> getResourceFiles(String path) throws IOException {
79-
List<String> filenames = new ArrayList<>();
80-
81-
try (
82-
InputStream in = getResourceAsStream(path);
83-
BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
84-
String resource;
85-
86-
while ((resource = br.readLine()) != null) {
87-
filenames.add(resource);
88-
}
89-
}
90-
91-
return filenames;
92-
}
93-
94-
private InputStream getResourceAsStream(String resource) {
95-
final InputStream in
96-
= getContextClassLoader().getResourceAsStream(resource);
97-
98-
return in == null ? getClass().getResourceAsStream(resource) : in;
99-
}
100-
101-
private ClassLoader getContextClassLoader() {
102-
return Thread.currentThread().getContextClassLoader();
103-
}
104-
105-
}
106-
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
package org.fugerit.java.fjdaogenquickstart;
22

3+
import org.fugerit.java.daogen.quickstart.config.QuickstartDBHelper;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
36
import org.springframework.boot.SpringApplication;
47
import org.springframework.boot.autoconfigure.SpringBootApplication;
58

69
@SpringBootApplication
710
public class FjDaogenQuickstartApplication {
811

12+
private static Logger logger = LoggerFactory.getLogger( FjDaogenQuickstartApplication.class );
13+
914
public static void main(String[] args) {
1015
SpringApplication.run(FjDaogenQuickstartApplication.class, args);
16+
logger.info( "app init start" );
17+
QuickstartDBHelper.init();
18+
logger.info( "app init end" );
1119
}
1220

1321
}

0 commit comments

Comments
 (0)