Skip to content

Commit a9a8cbc

Browse files
committed
Check for table-existance before trying to create them, to avoid unneccesarily requiring write-permissions
1 parent 77ca01c commit a9a8cbc

File tree

4 files changed

+57
-6
lines changed

4 files changed

+57
-6
lines changed

core/src/main/java/de/bluecolored/bluemap/core/storage/sql/commandset/AbstractCommandSet.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import com.github.benmanes.caffeine.cache.Caffeine;
2828
import com.github.benmanes.caffeine.cache.LoadingCache;
29+
import de.bluecolored.bluemap.core.logger.Logger;
2930
import de.bluecolored.bluemap.core.storage.compression.Compression;
3031
import de.bluecolored.bluemap.core.storage.sql.Database;
3132
import de.bluecolored.bluemap.core.util.Key;
@@ -35,8 +36,7 @@
3536

3637
import java.io.IOException;
3738
import java.sql.*;
38-
import java.util.ArrayList;
39-
import java.util.List;
39+
import java.util.*;
4040

4141
@SuppressWarnings("SqlSourceToSinkFlow")
4242
@RequiredArgsConstructor
@@ -53,6 +53,9 @@ public abstract class AbstractCommandSet implements CommandSet {
5353
protected final LoadingCache<Key, Integer> gridStorageKeys = Caffeine.newBuilder()
5454
.build(this::findOrCreateGridStorageKey);
5555

56+
@Language("sql")
57+
public abstract String listExistingTablesStatement();
58+
5659
@Language("sql")
5760
public abstract String createMapTableStatement();
5861

@@ -74,6 +77,27 @@ public abstract class AbstractCommandSet implements CommandSet {
7477
@Override
7578
public void initializeTables() throws IOException {
7679
db.run(connection -> {
80+
try {
81+
Set<String> tables = new HashSet<>(6);
82+
ResultSet result = executeQuery(connection, listExistingTablesStatement());
83+
while (result.next()) {
84+
tables.add(result.getString(1));
85+
}
86+
87+
if (tables.containsAll(Set.of(
88+
"bluemap_map",
89+
"bluemap_compression",
90+
"bluemap_item_storage",
91+
"bluemap_item_storage_data",
92+
"bluemap_grid_storage",
93+
"bluemap_grid_storage_data"
94+
))) return;
95+
} catch (SQLException ex) {
96+
Logger.global.logWarning("Failed to check for existing tables, will try to create them...");
97+
Logger.global.logDebug(ex.toString());
98+
}
99+
100+
// create tables (if not exists)
77101
executeUpdate(connection, createMapTableStatement());
78102
executeUpdate(connection, createCompressionTableStatement());
79103
executeUpdate(connection, createItemStorageTableStatement());
@@ -343,7 +367,6 @@ public String[] listMapIds(int start, int count) throws IOException {
343367

344368
public int mapKey(String mapId) {
345369
synchronized (mapKeys) {
346-
//noinspection DataFlowIssue
347370
return mapKeys.get(mapId);
348371
}
349372
}
@@ -379,7 +402,6 @@ public int findOrCreateMapKey(String mapId) throws IOException {
379402

380403
public int compressionKey(Compression compression) {
381404
synchronized (compressionKeys) {
382-
//noinspection DataFlowIssue
383405
return compressionKeys.get(compression);
384406
}
385407
}
@@ -415,7 +437,6 @@ public int findOrCreateCompressionKey(Compression compression) throws IOExceptio
415437

416438
public int itemStorageKey(Key key) {
417439
synchronized (itemStorageKeys) {
418-
//noinspection DataFlowIssue
419440
return itemStorageKeys.get(key);
420441
}
421442
}
@@ -451,7 +472,6 @@ public int findOrCreateItemStorageKey(Key key) throws IOException {
451472

452473
public int gridStorageKey(Key key) {
453474
synchronized (gridStorageKeys) {
454-
//noinspection DataFlowIssue
455475
return gridStorageKeys.get(key);
456476
}
457477
}

core/src/main/java/de/bluecolored/bluemap/core/storage/sql/commandset/MySQLCommandSet.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ public MySQLCommandSet(Database db) {
3333
super(db);
3434
}
3535

36+
@Override
37+
@Language("mysql")
38+
public String listExistingTablesStatement() {
39+
return """
40+
SELECT TABLE_NAME
41+
FROM information_schema.tables
42+
WHERE table_schema = DATABASE()
43+
AND table_type = 'BASE TABLE'
44+
""";
45+
}
46+
3647
@Override
3748
@Language("mysql")
3849
public String createMapTableStatement() {

core/src/main/java/de/bluecolored/bluemap/core/storage/sql/commandset/PostgreSQLCommandSet.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ public PostgreSQLCommandSet(Database db) {
3333
super(db);
3434
}
3535

36+
@Override
37+
@Language("postgresql")
38+
public String listExistingTablesStatement() {
39+
return """
40+
SELECT tablename
41+
FROM pg_catalog.pg_tables
42+
WHERE schemaname = current_schema()
43+
""";
44+
}
45+
3646
@Override
3747
@Language("postgresql")
3848
public String createMapTableStatement() {

core/src/main/java/de/bluecolored/bluemap/core/storage/sql/commandset/SqliteCommandSet.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ public SqliteCommandSet(Database db) {
3333
super(db);
3434
}
3535

36+
@Override
37+
@Language("sqlite")
38+
public String listExistingTablesStatement() {
39+
return """
40+
SELECT `name`
41+
FROM `sqlite_master`
42+
WHERE `type` = 'table'
43+
""";
44+
}
45+
3646
@Override
3747
@Language("sqlite")
3848
public String createMapTableStatement() {

0 commit comments

Comments
 (0)