Skip to content

Commit

Permalink
Fix initialization sql scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 committed Feb 10, 2024
1 parent b0cf9e0 commit f1adb96
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 48 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- maven wrapper

### Fixed

- initialization sql scripts

## [1.3.2] - 2023-12-27

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ public class QuickstartDBHelper {
private QuickstartDBHelper() {}

private static final Logger logger = LoggerFactory.getLogger( QuickstartDBHelper.class );

public static final String DEFAULT_DB_CONN_PATH = "quickstart_db/quickstart-db-conn.properties";
public static final String DEFAULT_DB_INIT_PATH = "quickstart_db/hsqldb";

private static final String DEFAULT_DB_CONN_PATH = "quickstart_db/quickstart-db-conn.properties";
private static final String DEFAULT_DB_INIT_PATH = "quickstart_db/hsqldb";

private static final String[] SQL_INIT_SCRIPTS = { "100_db_setup.sql", "200_sample_db.sql", "900_examples.sql" };

private static final String DRV = "db-mode-dc-drv";
private static final String URL = "db-mode-dc-url";
Expand All @@ -48,21 +50,19 @@ public static void init()
Properties props = new Properties();
props.load( is );
try ( Connection conn = newConnection( props ) ) {
cf = props;
ResScanner scanner = new ResScanner();
List<String> initFiles = scanner.getResourceFiles( DEFAULT_DB_INIT_PATH );
for ( String current : initFiles ) {
String res = DEFAULT_DB_INIT_PATH+"/"+current;
logger.info( "Current : {}", res );
try ( SQLScriptReader reader = new SQLScriptReader( QuickstartDBHelper.class.getClassLoader().getResourceAsStream( res ) ) ) {
SQLScriptFacade.executeAll(reader, conn);
cf = props;
}
}
}
for ( int k=0; k<SQL_INIT_SCRIPTS.length; k++ ) {
String current = SQL_INIT_SCRIPTS[k];
String res = DEFAULT_DB_INIT_PATH+"/"+current;
logger.info( "Current : {}", res );
try ( SQLScriptReader reader = new SQLScriptReader( QuickstartDBHelper.class.getClassLoader().getResourceAsStream( res ) ) ) {
SQLScriptFacade.executeAll(reader, conn);

}
}
}
cf = props;
}
} );

}
}

Expand All @@ -72,35 +72,3 @@ public static Connection newConnection() {
}

}

class ResScanner {

public List<String> getResourceFiles(String path) throws IOException {
List<String> filenames = new ArrayList<>();

try (
InputStream in = getResourceAsStream(path);
BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
String resource;

while ((resource = br.readLine()) != null) {
filenames.add(resource);
}
}

return filenames;
}

private InputStream getResourceAsStream(String resource) {
final InputStream in
= getContextClassLoader().getResourceAsStream(resource);

return in == null ? getClass().getResourceAsStream(resource) : in;
}

private ClassLoader getContextClassLoader() {
return Thread.currentThread().getContextClassLoader();
}

}

Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package org.fugerit.java.fjdaogenquickstart;

import org.fugerit.java.daogen.quickstart.config.QuickstartDBHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class FjDaogenQuickstartApplication {

private static Logger logger = LoggerFactory.getLogger( FjDaogenQuickstartApplication.class );

public static void main(String[] args) {
SpringApplication.run(FjDaogenQuickstartApplication.class, args);
logger.info( "app init start" );
QuickstartDBHelper.init();
logger.info( "app init end" );
}

}

0 comments on commit f1adb96

Please sign in to comment.