· 6 years ago · Aug 18, 2019, 03:28 PM
1package fr.Rgld_.Bungee.Helpers;
2
3import fr.Rgld_.Bungee.ChatRT;
4import net.dv8tion.jda.core.entities.Message;
5import net.dv8tion.jda.core.entities.TextChannel;
6import net.md_5.bungee.api.config.ServerInfo;
7
8import java.io.File;
9import java.io.IOException;
10import java.sql.*;
11import java.text.MessageFormat;
12import java.util.HashMap;
13
14public class Data {
15
16 private final ChatRT chatRT;
17 private final File file;
18 private final String TABLE_NAME_preference = "preference";
19 private final String TABLE_NAME_channels = "channels";
20
21 public Data(ChatRT chatRT) {
22 this.chatRT = chatRT;
23 file = new File(chatRT.getDataFolder(), "data.sqlite");
24 createPreferencesTable();
25 createChannelsTable();
26 }
27
28 private void createPreferencesTable() {
29 try (Connection connection = connect()) {
30 String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME_preference + "(id integer PRIMARY KEY,key text NOT NULL,value text NOT NULL);";
31 connection.createStatement().execute(sql);
32 } catch (SQLException | ClassNotFoundException e) {
33 e.printStackTrace();
34 }
35 }
36
37 private void createChannelsTable() {
38 try (Connection connection = connect()) {
39 String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME_channels + "(id integer PRIMARY KEY,server text NOT NULL,channelId long,selected boolean);";
40 connection.createStatement().execute(sql);
41 } catch (SQLException | ClassNotFoundException e) {
42 e.printStackTrace();
43 }
44 }
45
46 private Connection connect() throws SQLException, ClassNotFoundException {
47 if (!file.exists())
48 try {
49 file.createNewFile();
50 } catch (IOException e) {
51 e.printStackTrace();
52 }
53
54 Class.forName("org.sqlite.JDBC");
55 return DriverManager.getConnection("jdbc:sqlite:" + file.getAbsolutePath().replace("\\", File.separator));
56 }
57
58 public File getFile() {
59 return file;
60 }
61
62 private void put(String key, String value){
63 try (Connection connection = connect()){
64 String sql;
65 if(isIn(key)){
66 sql = MessageFormat.format("UPDATE {0} SET value = ? WHERE key = ?", TABLE_NAME_preference);
67 PreparedStatement psst = connection.prepareStatement(sql);
68 psst.setString(1, value);
69 psst.setString(2, key);
70 psst.executeUpdate();
71 } else {
72 sql = MessageFormat.format("INSERT INTO {0}(key,value) VALUES(?,?);", TABLE_NAME_preference);
73 PreparedStatement psst = connection.prepareStatement(sql);
74 psst.setString(1, key);
75 psst.setString(2, value);
76 psst.executeUpdate();
77 }
78 } catch (SQLException | ClassNotFoundException e) {
79 e.printStackTrace();
80 }
81 }
82
83 private boolean isIn(String key){
84 try (Connection connection = connect()) {
85 String sql = MessageFormat.format("SELECT key FROM {0} WHERE key = ?", TABLE_NAME_preference);
86 PreparedStatement psst = connection.prepareStatement(sql);
87 psst.setString(1, key);
88 return !psst.executeQuery().wasNull();
89 } catch (SQLException | ClassNotFoundException e) {
90 e.printStackTrace();
91 }
92 return false;
93 }
94
95 private String get(String key){
96 try (Connection connection = connect()){
97 String sql = MessageFormat.format("SELECT value FROM {0} WHERE key = ?", TABLE_NAME_preference);
98 PreparedStatement psst = connection.prepareStatement(sql);
99 psst.setString(1, key);
100 ResultSet rs = psst.executeQuery();
101 while (rs.next()) {
102 String value = rs.getString("value");
103 if(value != null){
104 return value;
105 }
106 }
107 } catch (SQLException | ClassNotFoundException e) {
108 e.printStackTrace();
109 }
110 return "error";
111 }
112
113 public void update(ServerInfo info, Long channelId, boolean selected){
114 try (Connection connection = connect()){
115 String sql;
116 if(serverIsEverSet(info.getName())){
117 sql = MessageFormat.format("UPDATE {0} SET channelId = ?,selected = ? WHERE server = ?", TABLE_NAME_channels);=
118 PreparedStatement psst = connection.prepareStatement(sql);
119 psst.setLong(1, channelId);
120 psst.setBoolean(2, selected);
121 psst.setString(3, info.getName());
122 psst.executeUpdate();
123 } else {
124 sql = "INSERT INTO {0}(server,channelId,selected) VALUES(?,?,?);";
125 PreparedStatement psst = connection.prepareStatement(sql);
126 psst.setString(1, info.getName());
127 psst.setLong(2, channelId);
128 psst.setBoolean(3, selected);
129 psst.executeUpdate();
130 }
131 } catch (SQLException | ClassNotFoundException e) {
132 e.printStackTrace();
133 }
134 }
135
136 public boolean serverIsEverSet(String server){
137 try (Connection connection = connect()) {
138 String sql = MessageFormat.format("SELECT channelId FROM {0} WHERE server = ?", TABLE_NAME_channels);
139 PreparedStatement psst = connection.prepareStatement(sql);
140 psst.setString(1, server);
141 return !psst.executeQuery().wasNull();
142 } catch(SQLException | ClassNotFoundException e){
143 e.printStackTrace();
144 }
145 return false;
146 }
147
148 public TextChannel getChannelByServer(ServerInfo serverInfo){
149 return getChannelByServer(serverInfo.getName());
150 }
151
152 public TextChannel getChannelByServer(String name){
153 try (Connection connection = connect()){
154 String sql = MessageFormat.format("SELECT channelId FROM {0} WHERE server = ?", TABLE_NAME_channels);
155 PreparedStatement psst = connection.prepareStatement(sql);
156 psst.setString(1, name);
157 ResultSet rs = psst.executeQuery();
158 while(rs.next()) {
159 Long id = rs.getLong("channelId");
160 return chatRT.getBot().getJda().getGuilds().get(0).getTextChannelById(id);
161 }
162 } catch (SQLException | ClassNotFoundException e) {
163 e.printStackTrace();
164 }
165 }
166
167
168 public HashMap<String, String> getAllDatas(){
169 HashMap<String, String> map = new HashMap<>();
170 try (Connection connection = connect()){
171 String sql = MessageFormat.format("SELECT key,value FROM {0}", TABLE_NAME_preference);
172 PreparedStatement psst = connection.prepareStatement(sql);
173 ResultSet rs = psst.executeQuery();
174 while(rs.next()){
175 map.put(rs.getString("key"), rs.getString("value"));
176 }
177 } catch (SQLException | ClassNotFoundException e) {
178 e.printStackTrace();
179 }
180 return map;
181 }
182}