Fixed outstanding buds, added 3 additional commands, added ConversationModule and Discord Utils for Role / Member search
This commit is contained in:
@@ -2,9 +2,20 @@ package io.rixa.bot.data.storage;
|
||||
|
||||
import io.rixa.bot.Rixa;
|
||||
import io.rixa.bot.data.config.Configuration;
|
||||
import io.rixa.bot.data.storage.enums.Statements;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.ResultSetExtractor;
|
||||
import org.springframework.jdbc.core.RowCountCallbackHandler;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
|
||||
import java.sql.Array;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
public class DatabaseAdapter {
|
||||
|
||||
private static DatabaseAdapter instance;
|
||||
@@ -22,9 +33,10 @@ public class DatabaseAdapter {
|
||||
Configuration config = rixaInstance.getConfiguration();
|
||||
DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
||||
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
|
||||
dataSource.setUrl(
|
||||
String.format("jdbc:mysql://%s:%s/%s", config.getSqlCredentials().get("hostName"),
|
||||
config.getSqlCredentials().get("port"), config.getSqlCredentials().get("databaseName")));
|
||||
String url = String.format("jdbc:mysql://%s:%s/%s", config.getSqlCredentials().get("hostName"),
|
||||
config.getSqlCredentials().get("port"), config.getSqlCredentials().get("databaseName"));
|
||||
System.out.println(url);
|
||||
dataSource.setUrl(url);
|
||||
dataSource.setUsername(config.getSqlCredentials().get("userName"));
|
||||
dataSource.setPassword(config.getSqlCredentials().get("password"));
|
||||
jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
@@ -36,8 +48,21 @@ public class DatabaseAdapter {
|
||||
}
|
||||
|
||||
public boolean exists(String table, String key, String value) {
|
||||
int amount = get().queryForObject(String.format("SELECT COUNT(*) FROM `%s` WHERE `%s` = ?", table, key), new Object[] { value }, Integer.class);
|
||||
return amount > 0;
|
||||
try {
|
||||
int amount = get().queryForObject
|
||||
(String.format(Statements.COUNT_CORE.getStatement(), table, key), new Object[]{value}, Integer.class);
|
||||
return amount > 0;
|
||||
} catch (EmptyResultDataAccessException ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public Array createArrayOf(String typeName, Object[] elements) {
|
||||
try {
|
||||
return get().getDataSource().getConnection().createArrayOf(typeName, elements);
|
||||
} catch (SQLException ignored) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static DatabaseAdapter getInstance() {
|
||||
|
||||
40
src/main/java/io/rixa/bot/data/storage/enums/Statements.java
Normal file
40
src/main/java/io/rixa/bot/data/storage/enums/Statements.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package io.rixa.bot.data.storage.enums;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
public enum Statements {
|
||||
|
||||
/*
|
||||
Select statements
|
||||
*/
|
||||
SELECT_CORE("SELECT * FROM `core` WHERE `guild_id` = ?"),
|
||||
SELECT_MODULE_STATUS("SELECT `{module_name}` FROM `modules` WHERE `guild_id` = ?"),
|
||||
|
||||
|
||||
/*
|
||||
Insert Statements
|
||||
*/
|
||||
INSERT_CORE("INSERT INTO `core` (`guild_id`, `guild_name`, `description`, `keywords`) VALUES (?, ?, ?, ?)"),
|
||||
|
||||
/*
|
||||
Count Statements
|
||||
*/
|
||||
COUNT_CORE("SELECT COUNT(*) FROM `%s` WHERE `%s` = ?");
|
||||
|
||||
/*
|
||||
Delete Statements
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
Create Table Statements
|
||||
*/
|
||||
|
||||
@Getter private String statement;
|
||||
public String getStatement(String key, String value) {
|
||||
return getStatement().replace(key, value);
|
||||
}
|
||||
Statements(String s) {
|
||||
this.statement = s;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user