Skip to content

Commit 693e349

Browse files
committed
Add ability to import from sqlite (experimental)
1 parent 4d7570a commit 693e349

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/main/java/net/coreprotect/database/convert/ClickhouseConverter.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public class ClickhouseConverter {
4444
private String mysqlUser;
4545
private String mysqlPassword;
4646

47+
private boolean sqlite = false;
48+
private String sqliteDatabasePath = "";
49+
4750
private final Map<String, TableData> tables = new LinkedHashMap<>();
4851
private final Path credentialsPath;
4952

@@ -80,6 +83,18 @@ public ClickhouseConverter(final CoreProtect plugin) {
8083
} catch (IOException | JsonSyntaxException e) {
8184
LOGGER.warn("Failed to read saved credentials file", e);
8285
}
86+
} else if (Files.exists(plugin.getDataPath().resolve("sqlite-database.json"))) {
87+
// Note: importing from sqlite is untested
88+
final Path path = plugin.getDataPath().resolve("sqlite-database.json");
89+
90+
try {
91+
final SQLiteDatabaseInformation information = JsonSerialization.DEFAULT_GSON.fromJson(Files.readString(path), SQLiteDatabaseInformation.class);
92+
93+
this.sqlite = true;
94+
this.sqliteDatabasePath = information.databasePath;
95+
} catch (IOException | JsonSyntaxException e) {
96+
LOGGER.warn("Failed to read sqlite database path", e);
97+
}
8398
}
8499
}
85100

@@ -111,6 +126,10 @@ public void login(String address, String database, String username, String passw
111126
}
112127

113128
public String formatMysqlSource(TableData table) {
129+
if (this.sqlite) {
130+
return "sqlite('" + this.sqliteDatabasePath + "', " + table.fullName() + "')";
131+
}
132+
114133
return "mysql('" + mysqlAddress + "', '" + mysqlDatabase + "', '" + table.fullName() + "', '" + mysqlUser + "', '" + mysqlPassword + "')";
115134
}
116135

@@ -157,4 +176,6 @@ public boolean next(ResultSet resultSet, PreparedStatement batchStatement, long
157176
}
158177

159178
private record MySQLLoginInformation(String address, String database, String user, String password) {}
179+
180+
private record SQLiteDatabaseInformation(String databasePath) {}
160181
}

0 commit comments

Comments
 (0)