From 7caec71de3e0b008e64f2847814682a7d712452b Mon Sep 17 00:00:00 2001 From: Florian Charlaix Date: Wed, 5 Jan 2022 09:05:11 +0100 Subject: [PATCH] Init database with tables and test user --- src/fr/univ/lyon1/server/Database.java | 17 +++++++++++++++++ .../univ/lyon1/server/models/ChannelModel.java | 2 +- .../lyon1/server/models/UserChannelModel.java | 2 +- src/fr/univ/lyon1/server/models/UserModel.java | 2 +- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/fr/univ/lyon1/server/Database.java b/src/fr/univ/lyon1/server/Database.java index b3651ab..8756122 100644 --- a/src/fr/univ/lyon1/server/Database.java +++ b/src/fr/univ/lyon1/server/Database.java @@ -1,5 +1,9 @@ package fr.univ.lyon1.server; +import fr.univ.lyon1.server.models.ChannelModel; +import fr.univ.lyon1.server.models.UserChannelModel; +import fr.univ.lyon1.server.models.UserModel; + import java.io.*; import java.sql.Connection; import java.sql.DriverManager; @@ -18,6 +22,8 @@ public class Database { err.printStackTrace(); System.exit(1); } + + init(); } private String[] getCredentials() throws NullPointerException, IOException { @@ -59,4 +65,15 @@ public class Database { return new Database(); return Database.database; } + + private void init() { + UserModel.generateTable(); + ChannelModel.generateTable(); + UserChannelModel.generateTable(); + + if (UserModel.get("test") == null) { + UserModel u = new UserModel("test", "test"); + u.save(); + } + } } diff --git a/src/fr/univ/lyon1/server/models/ChannelModel.java b/src/fr/univ/lyon1/server/models/ChannelModel.java index 3c744dc..a5d52fa 100644 --- a/src/fr/univ/lyon1/server/models/ChannelModel.java +++ b/src/fr/univ/lyon1/server/models/ChannelModel.java @@ -103,7 +103,7 @@ public class ChannelModel extends Channel implements Model { public static void generateTable() { try { - PreparedStatement ps = database.getConnection().prepareStatement("CREATE TABLE "+TABLE_NAME+" ( UUID varchar(40) primary key, name varchar(16) unique )"); + PreparedStatement ps = database.getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS "+TABLE_NAME+" ( UUID varchar(40) primary key, name varchar(16) unique )"); ps.executeUpdate(); } catch (SQLException err) { err.printStackTrace(); diff --git a/src/fr/univ/lyon1/server/models/UserChannelModel.java b/src/fr/univ/lyon1/server/models/UserChannelModel.java index 3858d62..8e7b728 100644 --- a/src/fr/univ/lyon1/server/models/UserChannelModel.java +++ b/src/fr/univ/lyon1/server/models/UserChannelModel.java @@ -87,7 +87,7 @@ public class UserChannelModel implements Model { public static void generateTable() { try { - PreparedStatement ps = database.getConnection().prepareStatement("CREATE TABLE "+TABLE_NAME+" (userUUID varchar(40) not null references User(UUID), channelUUID varchar(40) not null references Channel(UUID), PRIMARY KEY (userUUID, channelUUID))"); + PreparedStatement ps = database.getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS "+TABLE_NAME+" (userUUID varchar(40) not null references User(UUID), channelUUID varchar(40) not null references Channel(UUID), PRIMARY KEY (userUUID, channelUUID))"); ps.executeUpdate(); } catch (SQLException err) { err.printStackTrace(); diff --git a/src/fr/univ/lyon1/server/models/UserModel.java b/src/fr/univ/lyon1/server/models/UserModel.java index 95280db..4624640 100644 --- a/src/fr/univ/lyon1/server/models/UserModel.java +++ b/src/fr/univ/lyon1/server/models/UserModel.java @@ -118,7 +118,7 @@ public class UserModel extends User implements Model { public static void generateTable() { try { - PreparedStatement ps = database.getConnection().prepareStatement("CREATE TABLE "+TABLE_NAME+" ( UUID varchar(40) primary key, username varchar(16) unique, password varchar(256) )"); + PreparedStatement ps = database.getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS "+TABLE_NAME+" ( UUID varchar(40) primary key, username varchar(16) unique, password varchar(256) )"); ps.executeUpdate(); } catch (SQLException err) { err.printStackTrace();