This repository has been archived by the owner on Oct 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTablesRepositoryDAO.java
251 lines (215 loc) · 7.92 KB
/
TablesRepositoryDAO.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package databases.mysql.daos;
import databases.mysql.CloudEntityData;
import databases.mysql.DAO;
import databases.mysql.MySQLConnect;
import utility.PropertiesManager;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* Data Access Object for cloud NoSQL tables related information
*/
@SuppressWarnings({"DuplicatedCode", "SqlResolve", "RedundantSuppression"})
public class TablesRepositoryDAO extends DAO {
/**
* Queries
*/
private static final String CREATE_GOOGLE_TABLES_TABLE = "CREATE TABLE IF NOT EXISTS " +
PropertiesManager.getInstance().getProperty(PropertiesManager.MYSQL_DB) + ".google_cloud_tables (" +
"instance_id varchar(30) NOT NULL, " +
"table_name varchar(50) NOT NULL, " +
"PRIMARY KEY (instance_id)" +
")";
private static final String CREATE_AMAZON_TABLES_TABLE = "CREATE TABLE IF NOT EXISTS " +
PropertiesManager.getInstance().getProperty(PropertiesManager.MYSQL_DB) + ".amazon_cloud_tables (" +
"table_name varchar(50) NOT NULL, " +
"region varchar(15) NOT NULL, " +
"PRIMARY KEY (table_name)" +
")";
private static final String INSERT_GOOGLE_TABLE = "INSERT INTO " +
PropertiesManager.getInstance().getProperty(PropertiesManager.MYSQL_DB) + ".google_cloud_tables " +
"(instance_id, table_name) " + "VALUES (?, ?) " +
"ON DUPLICATE KEY UPDATE instance_id=VALUES(instance_id), table_name=VALUES(table_name)";
private static final String INSERT_AMAZON_TABLE = "INSERT INTO " +
PropertiesManager.getInstance().getProperty(PropertiesManager.MYSQL_DB) + ".amazon_cloud_tables " +
"(table_name, region) " + "VALUES (?, ?) " +
"ON DUPLICATE KEY UPDATE table_name=VALUES(table_name), region=VALUES(region)";
private static final String SELECT_GOOGLE_TABLES = "SELECT instance_id, table_name FROM " +
PropertiesManager.getInstance().getProperty(PropertiesManager.MYSQL_DB) + ".google_cloud_tables";
private static final String SELECT_AMAZON_TABLES = "SELECT table_name, region FROM " +
PropertiesManager.getInstance().getProperty(PropertiesManager.MYSQL_DB) + ".amazon_cloud_tables";
private static final String DROP_GOOGLE_TABLES = "DROP TABLE IF EXISTS " +
PropertiesManager.getInstance().getProperty(PropertiesManager.MYSQL_DB) + ".google_cloud_tables";
private static final String DROP_AMAZON_TABLES = "DROP TABLE IF EXISTS " +
PropertiesManager.getInstance().getProperty(PropertiesManager.MYSQL_DB) + ".amazon_cloud_tables";
/**
* Initializes tables
* @param connection database connection to use
* @param provider select which provider is needed to initialize corresponding tables
* @throws SQLException query definition and execution related problems
*/
private static void initTables(Connection connection, String provider) throws SQLException {
if (connection != null) {
initDatabase(connection);
Statement statement = connection.createStatement();
switch (provider) {
case GOOGLE:
statement.executeUpdate(CREATE_GOOGLE_TABLES_TABLE);
break;
case AMAZON:
statement.executeUpdate(CREATE_AMAZON_TABLES_TABLE);
break;
default:
statement.executeUpdate(CREATE_AMAZON_TABLES_TABLE);
statement.executeUpdate(CREATE_GOOGLE_TABLES_TABLE);
break;
}
statement.close();
} else {
System.err.println("Could not initialize database");
}
}
/**
* Drop every table associated to Google Cloud Platform NoSQL tables
*/
public static void dropGoogle() {
dropTable(GOOGLE);
}
/**
* Drop every table associated to Amazon Web Services NoSQL tables
*/
public static void dropAmazon() {
dropTable(AMAZON);
}
/**
* Generic drop table function
* @param provider select which provider is needed to delete corresponding tables
*/
private static void dropTable(String provider) {
try {
Connection connection = MySQLConnect.connectDatabase();
if (connection == null) {
System.err.println("Could not connect to database, please check your connection");
return;
}
Statement statement = connection.createStatement();
switch (provider) {
case GOOGLE:
statement.executeUpdate(DROP_GOOGLE_TABLES);
break;
case AMAZON:
statement.executeUpdate(DROP_AMAZON_TABLES);
break;
default:
System.err.println("Provider not supported! Could not perform DB drop");
}
statement.close();
MySQLConnect.closeConnection(connection);
} catch (SQLException e) {
System.err.println("Could not drop table(s): " + e.getMessage());
}
}
/**
* Persists a new Google Big Table table to database
* @param instanceId Big Table instance id
* @param tableName name of the table
*/
public static void persistGoogle(String instanceId, String tableName) {
try {
Connection connection = MySQLConnect.connectDatabase();
if (connection == null) {
System.err.println("Could not connect to database, please check your connection");
return;
}
initTables(connection, GOOGLE);
PreparedStatement preparedStatement = connection.prepareStatement(INSERT_GOOGLE_TABLE);
preparedStatement.setString(1, instanceId);
preparedStatement.setString(2, tableName);
preparedStatement.execute();
preparedStatement.close();
MySQLConnect.closeConnection(connection);
} catch (SQLException e) {
System.err.println("Could not perform insertion: " + e.getMessage());
}
}
/**
* Persists a new Amazon Dynamo DB table to database
* @param tableName name of the table
* @param region table deployment region
*/
public static void persistAmazon(String tableName, String region) {
try {
Connection connection = MySQLConnect.connectDatabase();
if (connection == null) {
System.err.println("Could not connect to database, please check your connection");
return;
}
initTables(connection, AMAZON);
PreparedStatement preparedStatement = connection.prepareStatement(INSERT_AMAZON_TABLE);
preparedStatement.setString(1, tableName);
preparedStatement.setString(2, region);
preparedStatement.execute();
preparedStatement.close();
MySQLConnect.closeConnection(connection);
} catch (SQLException e) {
System.err.println("Could not perform insertion: " + e.getMessage());
}
}
/**
* List every Google Big Table table
* @return list of tables (CloudEntityData)
*/
public static List<CloudEntityData> getGoogles() {
try {
Connection connection = MySQLConnect.connectDatabase();
if (connection == null) {
System.err.println("Could not connect to database, please check your connection");
return null;
}
initTables(connection, GOOGLE);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(SELECT_GOOGLE_TABLES);
List<CloudEntityData> result = new ArrayList<>();
while (resultSet.next()) {
result.add(new CloudEntityData(resultSet.getString("table_name"),
null,
resultSet.getString("instance_id")));
}
statement.close();
resultSet.close();
MySQLConnect.closeConnection(connection);
return result;
} catch (SQLException e) {
System.err.println("Could not perform select: " + e.getMessage());
return null;
}
}
/**
* List every Amazon Dynamo DB table
* @return list of tables (CloudEntityData)
*/
public static List<CloudEntityData> getAmazons() {
try {
Connection connection = MySQLConnect.connectDatabase();
if (connection == null) {
System.err.println("Could not connect to database, please check your connection");
return null;
}
initTables(connection, AMAZON);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(SELECT_AMAZON_TABLES);
List<CloudEntityData> result = new ArrayList<>();
while (resultSet.next()) {
result.add(new CloudEntityData(resultSet.getString("table_name"),
resultSet.getString("region")));
}
statement.close();
resultSet.close();
MySQLConnect.closeConnection(connection);
return result;
} catch (SQLException e) {
System.err.println("Could not perform select: " + e.getMessage());
return null;
}
}
}