-
Notifications
You must be signed in to change notification settings - Fork 16
/
CustomSQLModel.java
343 lines (301 loc) · 10.6 KB
/
CustomSQLModel.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import java.util.Map;
import java.sql.*;
import java.net.*;
import java.io.*;
import java.util.ArrayList;
public class CustomSQLModel
{
String id;
String tableName;
ArrayList<String> columns;
ArrayList<String> columnDataTypes;
String sqlText;
String sourceType;
String sourceServerName;
String sourceInstanceName;
String sourcePort;
String sourceDatabaseName;
String sourceUserName;
String sourcePass;
String gpfdistPort;
public static int maxColumns = 50;
public static ResultSet getList(String search, String limit, String offset, String sortBy, String sort) throws SQLException
{
String strSQL = "SELECT '<button onclick=\"updateCustomSQL(''' || id || ''', ''update'')\">Update</button>' ||\n";
strSQL += "' <button onclick=\"updateCustomSQL(''' || id || ''', ''delete'')\">Delete</button>' AS manage,\n";
strSQL += "id,\n";
strSQL += "LOWER(table_name) AS table_name, initcap(source_type) as source_type\n";
strSQL += "FROM os.custom_sql\n";
if (!search.equals(""))
{
strSQL += "WHERE LOWER(table_name) LIKE '%' || LOWER('" + search + "') || '%'\n";
strSQL += "OR LOWER(source_type) LIKE '%' || LOWER('" + search + "') || '%'\n";
}
sortBy = sortBy.toLowerCase();
if (sortBy.equals("table_name") || sortBy.equals("source_type"))
strSQL += "ORDER BY " + sortBy + " " + sort + "\n";
else
strSQL += "ORDER BY LOWER(table_name) ASC\n";
if (!limit.equals(""))
strSQL += "LIMIT " + limit + " ";
if (!offset.equals(""))
strSQL += "OFFSET " + offset;
try
{
ResultSet rs = OutsourcerModel.getResults(strSQL);
return rs;
}
catch (SQLException ex)
{
throw new SQLException(ex.getMessage());
}
}
private static void customStop(String id) throws SQLException
{
int myGpfdistPort = 0;
try
{
//get the existing gpfdist_port value
//but only if the port isn't in use by another
//external table.
//two tables should never use the same port
//unless there is a problem
String strSQL = "SELECT c1.gpfdist_port\n";
strSQL += "FROM os.custom_sql AS c1\n";
strSQL += "WHERE c1.id = " + id + "\n";
strSQL += "AND NOT EXISTS (SELECT NULL\n";
strSQL += " FROM os.custom_sql AS c2\n";
strSQL += " WHERE c1.id <> c2.id\n";
strSQL += " AND c1.gpfdist_port = c2.gpfdist_port)";
ResultSet rs = OutsourcerModel.getResults(strSQL);
while (rs.next())
{
myGpfdistPort = rs.getInt(1);
GpfdistRunner.customStop(OSProperties.osHome, myGpfdistPort);
}
}
catch (SQLException ex)
{
throw new SQLException(ex.getMessage());
}
}
public static void updateTable(String id, String tableName, ArrayList<String> columns, ArrayList<String> columnDataTypes, String sqlText, String sourceType, String sourceServerName, String sourceInstanceName, String sourcePort, String sourceDatabaseName, String sourceUserName, String sourcePass) throws SQLException
{
String strColumns = OutsourcerModel.javaArrayToString(columns);
String strColumnDataTypes = OutsourcerModel.javaArrayToString(columnDataTypes);
sqlText = OutsourcerModel.setSQLString(sqlText);
sourceType = OutsourcerModel.setSQLString(sourceType);
sourceServerName = OutsourcerModel.setSQLString(sourceServerName);
sourceInstanceName = OutsourcerModel.setSQLString(sourceInstanceName);
sourcePort = OutsourcerModel.setSQLInt(sourcePort);
sourceDatabaseName = OutsourcerModel.setSQLString(sourceDatabaseName);
sourceUserName = OutsourcerModel.setSQLString(sourceUserName);
sourcePass = OutsourcerModel.setSQLString(sourcePass);
try
{
//stop the gpfdist process for this job
customStop(id);
//drop the old version of the table
tableName = tableName.toLowerCase();
dropExtTable(tableName);
//add the quotes for the table name so the insert works properly
tableName = OutsourcerModel.setSQLString(tableName);
//dynamically get the gpfdist port for the custom table
int gpfdistPort = GpfdistRunner.customStart(OSProperties.osHome);
//build the insert statement
String strSQL = "INSERT INTO os.ao_custom_sql\n";
strSQL += "(id, table_name, columns, column_datatypes, sql_text, source_type, source_server_name, source_instance_name, source_port, source_database_name, source_user_name, source_pass, gpfdist_port)\n";
strSQL += "VALUES\n";
strSQL += " (" + id + ", \n";
strSQL += " " + tableName + ", \n";
strSQL += " " + strColumns + ", \n";
strSQL += " " + strColumnDataTypes + ", \n";
strSQL += " " + sqlText + ", \n";
strSQL += " " + sourceType + ", \n";
strSQL += " " + sourceServerName + ", \n";
strSQL += " " + sourceInstanceName + ", \n";
strSQL += " " + sourcePort + ", \n";
strSQL += " " + sourceDatabaseName + ", \n";
strSQL += " " + sourceUserName + ", \n";
strSQL += " " + sourcePass + ", \n";
strSQL += " " + gpfdistPort + ")\n";
OutsourcerModel.updateTable(strSQL);
//create the external table
createExtTable(id);
}
catch (SQLException ex)
{
throw new SQLException(ex.getMessage());
}
}
public static void deleteTable(String id, String tableName) throws SQLException
{
String strSQL = "INSERT INTO os.ao_custom_sql\n";
strSQL += "(id, table_name, columns, column_datatypes, sql_text, source_type, source_server_name, source_instance_name, source_port, source_database_name, source_user_name, source_pass, gpfdist_port, deleted)\n";
strSQL += "SELECT id, table_name, columns, column_datatypes, sql_text, source_type, source_server_name, source_instance_name, source_port, source_database_name, source_user_name, source_pass, gpfdist_port, TRUE AS deleted\n";
strSQL += "FROM os.custom_sql\n";
strSQL += "WHERE id = " + id;
try
{
//stop the gpfdist process for this job
customStop(id);
tableName = tableName.toLowerCase();
dropExtTable(tableName);
OutsourcerModel.updateTable(strSQL);
}
catch (SQLException ex)
{
throw new SQLException(ex.getMessage());
}
}
public static void insertTable(String tableName, ArrayList<String> columns, ArrayList<String> columnDataTypes, String sqlText, String extConnectionId) throws SQLException
{
tableName = OutsourcerModel.setSQLString(tableName.toLowerCase());
String strColumns = OutsourcerModel.javaArrayToString(columns);
String strColumnDataTypes = OutsourcerModel.javaArrayToString(columnDataTypes);
sqlText = OutsourcerModel.setSQLString(sqlText);
try
{
String id = nextVal();
//dynamically get the gpfdist port for the custom table
int gpfdistPort = GpfdistRunner.customStart(OSProperties.osHome);
String strSQL = "INSERT INTO os.ao_custom_sql\n";
strSQL += "(id, table_name, columns, column_datatypes, sql_text, gpfdist_port,\n";
strSQL += "source_type, source_server_name, source_instance_name, source_port, source_database_name, source_user_name, source_pass)\n";
strSQL += "SELECT " + id + ", " + tableName + ", " + strColumns + ", " + strColumnDataTypes + ", " + sqlText + ", " + gpfdistPort + ",\n";
strSQL += "source_type, source_server_name, source_instance_name, source_port, source_database_name, source_user_name, source_pass\n";
strSQL += "FROM os.ext_connection\n";
strSQL += "WHERE id = " + extConnectionId;
OutsourcerModel.updateTable(strSQL);
createExtTable(id);
}
catch (SQLException ex)
{
throw new SQLException(ex.getMessage());
}
}
public static ArrayList<String> getDataTypes()
{
ArrayList<String> dataTypes = new ArrayList<String>();
dataTypes.add("bigint");
dataTypes.add("boolean");
dataTypes.add("float8");
dataTypes.add("int");
dataTypes.add("numeric");
dataTypes.add("smallint");
dataTypes.add("text");
dataTypes.add("timestamp");
dataTypes.add("timestamptz");
dataTypes.add("varchar");
return dataTypes;
}
private static String nextVal() throws SQLException
{
try
{
String id = "";
String strSQL = "SELECT nextval('os.ao_custom_sql_id_seq')";
OutsourcerModel.getResults(strSQL);
ResultSet rs = OutsourcerModel.getResults(strSQL);
while (rs.next())
{
id = Integer.toString(rs.getInt(1));
}
return id;
}
catch (SQLException ex)
{
throw new SQLException(ex.getMessage());
}
}
private static void createExtTable(String id) throws SQLException
{
try
{
String strSQL = "SELECT LOWER(table_name) as table_name,\n";
strSQL += "unnest(columns) AS column_name,\n";
strSQL += "unnest(column_datatypes) AS data_type,\n";
strSQL += "gpfdist_port\n";
strSQL += "FROM os.custom_sql\n";
strSQL += "WHERE id = " + id;
String strSQLCreateTable = "";
int i = 0;
int gpfdistPort = 0;
ResultSet rs = OutsourcerModel.getResults(strSQL);
while (rs.next())
{
i++;
if (i == 1)
{
gpfdistPort = rs.getInt(4);
strSQLCreateTable = "CREATE EXTERNAL TABLE " + rs.getString(1) + "\n";
strSQLCreateTable += "(" + rs.getString(2) + " " + rs.getString(3);
}
else
{
strSQLCreateTable += ",\n" + "\"" + rs.getString(2) + "\" " + rs.getString(3);
}
}
strSQLCreateTable += ")\n";
strSQLCreateTable += "LOCATION ('gpfdist://" + OSProperties.osServer + ":" + gpfdistPort + "/config.properties+" + id + "#transform=externaldata')\n";
strSQLCreateTable += "FORMAT 'TEXT' (delimiter '|' null 'null')";
OutsourcerModel.updateTable(strSQLCreateTable);
}
catch (SQLException ex)
{
throw new SQLException(ex.getMessage());
}
}
private static void dropExtTable(String tableName) throws SQLException
{
try
{
String strSQL = "DROP EXTERNAL TABLE IF EXISTS " + tableName;
OutsourcerModel.updateTable(strSQL);
}
catch (SQLException ex)
{
throw new SQLException(ex.getMessage());
}
}
public CustomSQLModel (String aId) throws SQLException
{
String strSQL = "SELECT id, table_name, columns, column_datatypes, sql_text, source_type, source_server_name, source_instance_name, source_port, source_database_name, source_user_name, source_pass\n";
strSQL += "FROM os.custom_sql\n";
strSQL += "WHERE id = " + aId;
try
{
ResultSet rs = OutsourcerModel.getResults(strSQL);
while (rs.next())
{
id = Integer.toString(rs.getInt(1));
tableName = rs.getString(2);
columns = OutsourcerModel.gpArrayToJavaArray(rs.getString(3));
columnDataTypes = OutsourcerModel.gpArrayToJavaArray(rs.getString(4));
sqlText = rs.getString(5);
sourceType = rs.getString(6);
sourceServerName = rs.getString(7);
sourceInstanceName = rs.getString(8);
sourcePort = rs.getString(9);
sourceDatabaseName = rs.getString(10);
sourceUserName = rs.getString(11);
sourcePass = rs.getString(12);
}
}
catch (SQLException ex)
{
//do something??
}
}
public static CustomSQLModel getModel(String id)
{
try
{
return new CustomSQLModel(id);
}
catch (Exception ex)
{
return null;
}
}
}