Skip to content

Commit

Permalink
Fix DatabaseInitializer
Browse files Browse the repository at this point in the history
  • Loading branch information
everoddandeven committed Dec 23, 2024
1 parent c95db46 commit 10cbacc
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 15 deletions.
95 changes: 81 additions & 14 deletions src/main/java/monero/ecwid/server/DatabaseInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.Statement;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class DatabaseInitializer {
private static final Logger logger = LoggerFactory.getLogger(DatabaseInitializer.class);
private static final String customDelimiter = "$$";

private final JdbcTemplate jdbcTemplate;

Expand Down Expand Up @@ -53,8 +54,8 @@ public void initializeDatabase() {
}

executeSqlFiles(List.of(databaseScript, functionsScript, proceduresScript, viewsScript, triggersScript, eventsScript));

logger.info("Successfully created monero_ecwid database");

} catch (Exception e) {
String msg = e.getMessage();

Expand All @@ -77,25 +78,91 @@ private boolean databaseExists() {
String query = "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = ?";
String result = jdbcTemplate.queryForObject(query, new Object[]{dbName}, String.class);

return result != null;
if (result == null) {
return false;
}

String checkTableQuery = "SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ? AND TABLE_NAME IN (?, ?)";
Integer tableCount = jdbcTemplate.queryForObject(checkTableQuery, new Object[]{dbName, "payment_requests", "monero_transactions"}, Integer.class);

if (tableCount == null) {
return false;
}
else if (tableCount.equals(Integer.valueOf(2))) {
logger.info("Database monero_ecwid exists.");
return true;
} else {
logger.warn("Database is empty.");
return false;
}
} catch (Exception e) {
return false;
}
}

private void executeSqlFiles(List<Resource> sqlFiles) throws Exception {
for (Resource sqlFile : sqlFiles) {
String sql = readResource(sqlFile);
if (!sql.isBlank()) {
jdbcTemplate.execute(sql);
logger.info("Executed SQL file: " + sqlFile.getFilename());
private void executeStatement(Statement statement, String sql) throws Exception {
statement.execute(sql.replace(customDelimiter, ""));
}

private void executeSqlFile(Resource resource) throws Exception {
try (
Connection connection = jdbcTemplate.getDataSource().getConnection();
Statement statement = connection.createStatement();
BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream()))
) {
StringBuilder sqlBuilder = new StringBuilder();
String line;

boolean insideDelimiter = false;

while ((line = reader.readLine()) != null) {
line = line.trim();

if (line.isEmpty() || line.startsWith("--") || line.startsWith("#")) {
continue;
}

if (line.toUpperCase().startsWith("DELIMITER")) {
if (insideDelimiter) {
insideDelimiter = false;

if (sqlBuilder.length() != 0) {
String sql = sqlBuilder.toString().trim();
executeStatement(statement, sql);
System.out.println("Eseguito blocco SQL:\n" + sql);
sqlBuilder.setLength(0);
}
}
else {
insideDelimiter = true;
}

continue;
}

sqlBuilder.append(line).append(" ");

if (line.endsWith(";") && ! insideDelimiter) {
String sql = sqlBuilder.toString().trim();
executeStatement(statement, sql);
sqlBuilder.setLength(0);
}
}

if (sqlBuilder.length() != 0) {
String sql = sqlBuilder.toString().trim();
executeStatement(statement, sql);
sqlBuilder.setLength(0);
}
} catch (Exception e) {
throw new Exception("An error occured while executing sql file SQL: " + resource.getFilename(), e);
}
}

private String readResource(Resource resource) throws Exception {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8))) {
return reader.lines().collect(Collectors.joining("\n"));
private void executeSqlFiles(List<Resource> sqlFiles) throws Exception {
for (Resource sqlFile : sqlFiles) {
executeSqlFile(sqlFile);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static String getDataSourceUrl() {

String host = config.dbHost;
String port = config.dbPort.toString();
return String.format("jdbc:mysql://%s:%s/monero_ecwid", host, port);
return String.format("jdbc:mysql://%s:%s/monero_ecwid?createDatabaseIfNotExist=true", host, port);
}

@Bean
Expand Down

0 comments on commit 10cbacc

Please sign in to comment.