Skip to content

Commit

Permalink
feat: adding auto creation of sqlite db folder
Browse files Browse the repository at this point in the history
  • Loading branch information
ohager committed Dec 1, 2024
1 parent 2d1d58e commit 406766d
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions src/brs/db/sql/dialects/DatabaseInstanceSqlite.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,31 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

public class DatabaseInstanceSqlite extends DatabaseInstanceBaseImpl {
private static final Logger logger = LoggerFactory.getLogger(DatabaseInstanceSqlite.class);

protected DatabaseInstanceSqlite(PropertyService propertyService) {
super(propertyService);
}

private String getJournalMode(){
private String getJournalMode() {
String journalMode = propertyService.getString(Props.DB_SQLITE_JOURNAL_MODE).toUpperCase();
if(
if (
journalMode.equals("WAL") ||
journalMode.equals("TRUNCATE") ||
journalMode.equals("DELETE") ||
journalMode.equals("PERSIST")
){
journalMode.equals("TRUNCATE") ||
journalMode.equals("DELETE") ||
journalMode.equals("PERSIST")
) {
return journalMode;
}
return "WAL";
}

private String getSynchronousMode(){
private String getSynchronousMode() {
String synchronous = propertyService.getString(Props.DB_SQLITE_SYNCHRONOUS).toUpperCase();
switch (synchronous) {
case "FULL":
Expand All @@ -40,7 +44,7 @@ private String getSynchronousMode(){
}
}

private int getCacheSize(){
private int getCacheSize() {
return propertyService.getInt(Props.DB_SQLITE_CACHE_SIZE);
}

Expand Down Expand Up @@ -69,9 +73,34 @@ public SQLDialect getDialect() {
}


private static String extractSqliteFolderPath(String jdbcUrl) {
if (jdbcUrl == null || !jdbcUrl.startsWith("jdbc:sqlite:")) {
throw new IllegalArgumentException("Invalid SQLite JDBC URL");
}
String filePath = jdbcUrl.substring("jdbc:sqlite:".length());
Path path = Paths.get(filePath).toAbsolutePath().getParent();
return path != null ? path.toString() : null;
}

@Override
protected void onStartupImpl() {

String dbUrl = propertyService.getString(Props.DB_URL);
String folderPath = extractSqliteFolderPath(dbUrl);
if (folderPath != null) {
// get the folder from url!
File dbFolder = new File(folderPath);
if (!dbFolder.exists()) {
logger.info("Creating SQLite DB folder: " + folderPath);
boolean created = dbFolder.mkdirs(); // creates parent directories if needed
if (!created) {
logger.warn("Failed to create SQLite DB folder: " + folderPath);
}
} else {
logger.warn("SQLite database folder path couldn't be found for " + dbUrl);
}
}
// get the folder from url!
if (propertyService.getBoolean(Props.DB_OPTIMIZE)) {
logger.info("SQLite optimization started...");
executeSQL("PRAGMA optimize");
Expand Down

0 comments on commit 406766d

Please sign in to comment.