Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SQLite support #63

Merged
merged 1 commit into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import dev.pgm.community.Community;
import dev.pgm.community.assistance.Report;
import dev.pgm.community.feature.SQLFeatureBase;
import dev.pgm.community.utils.DatabaseUtils;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -59,7 +60,7 @@ public CompletableFuture<List<Report>> queryList(String target) {
String id = row.getString("id");
String sender = row.getString("sender");
String reason = row.getString("reason");
long time = Long.parseLong(row.getString("time"));
long time = DatabaseUtils.parseLong(row, "time");
reports
.getReports()
.add(new Report(
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/dev/pgm/community/database/DatabaseConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class DatabaseConfig {
private String databaseName;
private String timezone;
private int maxConnections;
private String sqliteFileName;

public DatabaseConfig(Configuration config) {
reload(config);
Expand All @@ -24,6 +25,7 @@ public void reload(Configuration config) {
this.databaseName = config.getString("database.databaseName");
this.timezone = config.getString("database.timezone");
this.maxConnections = config.getInt("database.max-connections");
this.sqliteFileName = config.getString("database.sqlite-file");
}

public boolean isEnabled() {
Expand Down Expand Up @@ -51,6 +53,10 @@ public String getTimezone() {
}

public int getMaxDatabaseConnections() {
return maxConnections;
return isEnabled() ? maxConnections : 1;
}

public String getSQLiteFileName() {
return sqliteFileName;
}
}
42 changes: 24 additions & 18 deletions src/main/java/dev/pgm/community/database/DatabaseConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import co.aikar.idb.BukkitDB;
import co.aikar.idb.DatabaseOptions;
import co.aikar.idb.DatabaseOptions.DatabaseOptionsBuilder;
import co.aikar.idb.PooledDatabaseOptions;
import co.aikar.idb.PooledDatabaseOptions.PooledDatabaseOptionsBuilder;
import com.google.common.collect.Maps;
import dev.pgm.community.Community;
import java.util.Map;
Expand All @@ -17,25 +19,29 @@ public DatabaseConnection(Community plugin) {
Map<String, Object> extraOptions = Maps.newHashMap();
extraOptions.put("serverTimezone", config.getTimezone());

DatabaseOptions options =
DatabaseOptions.builder()
.poolName(plugin.getDescription().getName() + " DB")
.logger(plugin.getLogger())
.mysql(
config.getUsername(),
config.getPassword(),
config.getDatabaseName(),
config.getHost())
.build();

PooledDatabaseOptions poolOptions =
PooledDatabaseOptions.builder()
.options(options)
.maxConnections(config.getMaxDatabaseConnections())
.dataSourceProperties(extraOptions)
.build();
DatabaseOptionsBuilder builder = DatabaseOptions.builder()
.poolName(plugin.getDescription().getName() + " DB")
.logger(plugin.getLogger());

if (config.isEnabled()) {
builder.mysql(
config.getUsername(), config.getPassword(), config.getDatabaseName(), config.getHost());
} else {
builder.sqlite(config.getSQLiteFileName());
builder.minAsyncThreads(1);
builder.maxAsyncThreads(1);
}

PooledDatabaseOptionsBuilder poolBuilder = PooledDatabaseOptions.builder()
.options(builder.build())
.maxConnections(config.getMaxDatabaseConnections());

// Apply extra MySQL options
if (config.isEnabled()) {
poolBuilder.dataSourceProperties(extraOptions);
}

// Setup the main global DB
BukkitDB.createHikariDatabase(plugin, poolOptions);
BukkitDB.createHikariDatabase(plugin, poolBuilder.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import dev.pgm.community.feature.SQLFeatureBase;
import dev.pgm.community.friends.Friendship;
import dev.pgm.community.friends.Friendship.FriendshipStatus;
import dev.pgm.community.utils.DatabaseUtils;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -23,15 +24,13 @@ public class SQLFriendshipService extends SQLFeatureBase<Friendship, String>

public SQLFriendshipService() {
super(TABLE_NAME, TABLE_FIELDS);
this.friendshipCache =
CacheBuilder.newBuilder()
.build(
new CacheLoader<UUID, PlayerFriendships>() {
@Override
public PlayerFriendships load(UUID key) throws Exception {
return new PlayerFriendships(key);
}
});
this.friendshipCache = CacheBuilder.newBuilder()
.build(new CacheLoader<UUID, PlayerFriendships>() {
@Override
public PlayerFriendships load(UUID key) throws Exception {
return new PlayerFriendships(key);
}
});
}

@Override
Expand Down Expand Up @@ -97,35 +96,33 @@ public CompletableFuture<List<Friendship>> queryList(String target) {
return CompletableFuture.completedFuture(new ArrayList<>(playerFriendships.getFriendships()));
} else {
return DB.getResultsAsync(SELECT_FRIENDSHIPS_QUERY, playerId.toString(), playerId.toString())
.thenApplyAsync(
results -> {
if (results != null) {
for (DbRow row : results) {
String id = row.getString("id");
String requester = row.getString("requester");
String requested = row.getString("requested");
String status = row.getString("status");
long requestDate = Long.parseLong(row.getString("requestDate"));
long updateDate = Long.parseLong(row.getString("updateDate"));

Instant requestInstant = Instant.ofEpochMilli(requestDate);
Instant updateInstant = Instant.ofEpochMilli(updateDate);

playerFriendships
.getFriendships()
.add(
new Friendship(
UUID.fromString(id),
UUID.fromString(requester),
UUID.fromString(requested),
FriendshipStatus.valueOf(status.toUpperCase()),
requestInstant,
updateInstant));
}
}
playerFriendships.setLoaded(true);
return new ArrayList<>(playerFriendships.getFriendships());
});
.thenApplyAsync(results -> {
if (results != null) {
for (DbRow row : results) {
String id = row.getString("id");
String requester = row.getString("requester");
String requested = row.getString("requested");
String status = row.getString("status");
long requestDate = DatabaseUtils.parseLong(row, "requestDate");
long updateDate = DatabaseUtils.parseLong(row, "updateDate");

Instant requestInstant = Instant.ofEpochMilli(requestDate);
Instant updateInstant = Instant.ofEpochMilli(updateDate);

playerFriendships
.getFriendships()
.add(new Friendship(
UUID.fromString(id),
UUID.fromString(requester),
UUID.fromString(requested),
FriendshipStatus.valueOf(status.toUpperCase()),
requestInstant,
updateInstant));
}
}
playerFriendships.setLoaded(true);
return new ArrayList<>(playerFriendships.getFriendships());
});
}
}

Expand Down
Loading