-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Starboard entry to Database [#126]
- Loading branch information
1 parent
cf73748
commit 812ee98
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package de.progen_bot.db.dao.config; | ||
|
||
import de.progen_bot.db.entities.config.GuildConfiguration; | ||
import net.dv8tion.jda.api.entities.Guild; | ||
|
||
public interface ConfigDao { | ||
|
||
void writeConfig(GuildConfiguration configuration, Guild guild); | ||
GuildConfiguration loadConfig(Guild guild); | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/de/progen_bot/db/dao/config/ConfigDaoImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package de.progen_bot.db.dao.config; | ||
|
||
import de.progen_bot.db.connection.ConnectionFactory; | ||
import de.progen_bot.db.dao.Dao; | ||
import de.progen_bot.db.entities.config.GuildConfiguration; | ||
import de.progen_bot.db.entities.config.GuildConfigurationBuilder; | ||
import net.dv8tion.jda.api.entities.Guild; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
public class ConfigDaoImpl extends Dao implements ConfigDao { | ||
|
||
@Override | ||
public void writeConfig(GuildConfiguration configuration , Guild guild) { | ||
Connection connection = ConnectionFactory.getConnection(); | ||
try { | ||
PreparedStatement ps = connection.prepareStatement("REPLACE INTO `config` (`guildid`, `prefix`, " + | ||
"`logChannelID`, `tempChannelCategoryID`, `autorole`, `starBoardChannelID`) VALUES (?, ?, ?, ?, ?, ?);"); | ||
ps.setString(1 , guild.getId()); | ||
ps.setString(2 , configuration.getPrefix()); | ||
ps.setString(3 , configuration.getLogChannelID()); | ||
ps.setString(4 , configuration.getTempChannelCategoryID()); | ||
ps.setString(5 , configuration.getAutoRole()); | ||
ps.setString(6, configuration.getStarBoardChannelID()); | ||
ps.execute(); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public GuildConfiguration loadConfig(Guild guild) { | ||
Connection connection = ConnectionFactory.getConnection(); | ||
try { | ||
PreparedStatement ps = connection.prepareStatement("SELECT * FROM `config` WHERE `guildid` = ?;"); | ||
ps.setString(1 , guild.getId()); | ||
ResultSet rs = ps.executeQuery(); | ||
|
||
if (rs.next()) { | ||
return new GuildConfigurationBuilder().setPrefix(rs.getString("prefix")) | ||
.setLogChannelID(rs.getString("logChannelID")) | ||
.setTempChannelCategoryID(rs.getString("tempChannelCategoryID")) | ||
.setStarBoardChannelID(rs.getString("starBoardChannelID")) | ||
.setAutorole(rs.getString("autorole")).build(); | ||
} | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public void generateTables(String query) { | ||
String sqlQuery = "CREATE TABLE IF NOT EXISTS config (guildid VARCHAR(18) NOT NULL, prefix VARCHAR" + | ||
"(18) NOT NULL , logChannelID VARCHAR(18), starBoardChannelID VARCHAR(18), tempChannelCategoryID VARCHAR(18), autorole VARCHAR(18), UNIQUE " + | ||
"(guildid)) ENGINE = InnoDB;"; | ||
super.generateTables(sqlQuery); | ||
} | ||
} |