Archived
1
0
Fork 0

Init database with tables and test user

This commit is contained in:
Ethanell 2022-01-05 09:05:11 +01:00
parent f8a5cde8bf
commit 7caec71de3
4 changed files with 20 additions and 3 deletions

View file

@ -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();
}
}
}

View file

@ -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();

View file

@ -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();

View file

@ -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();