Skip to content

Commit

Permalink
create database tables
Browse files Browse the repository at this point in the history
  • Loading branch information
ryderbelserion committed Dec 17, 2024
1 parent 2a70e73 commit 73018f3
Showing 1 changed file with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.CompletableFuture;

public class SqliteConnector implements Connector {

Expand All @@ -30,6 +32,9 @@ public Connector init(final File file) {
return this;
}

public static final String user_table_creation = "create table if not exists user(user_id varchar(32) primary key, total_crates_opened int)";
public static final String crate_table_creation = "create table if not exists crate(user_id varchar(32) primary key, crate_name varchar(16) primary key, amount int, times_opened int, current_respins int, foreign key (user_id) references user(user_id))";

@Override
public void start() {
final HikariConfig config = new HikariConfig();
Expand All @@ -39,6 +44,21 @@ public void start() {
config.setConnectionInitSql("PRAGMA foreign_keys = ON;");

this.source = new HikariDataSource(config);

CompletableFuture.runAsync(() -> {
try (final Connection connection = getConnection()) {
if (connection == null || connection.isClosed()) return;

try (final Statement statement = connection.createStatement()) {
statement.executeUpdate(user_table_creation);
statement.executeUpdate(crate_table_creation);
} catch (final SQLException exception) {
throw new CratesException("Failed to create user table.", exception);
}
} catch (final SQLException exception) {
throw new CratesException("Failed to create connection", exception);
}
});
}

@Override
Expand Down

0 comments on commit 73018f3

Please sign in to comment.