forked from RunningJon/outsourcer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomSQL.java
executable file
·289 lines (251 loc) · 7.97 KB
/
CustomSQL.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
import java.sql.*;
import java.net.*;
import java.io.*;
public class CustomSQL
{
private static String myclass = "CustomSQL";
public static boolean debug = true;
public static String configFile = "";
public static void main(String[] args) throws Exception
{
String method = "main";
int location = 1000;
int argsCount = args.length;
configFile = args[0];
String action = args[1];
Connection conn = null;
location = 2000;
try
{
location = 3000;
if (action.equals("start"))
{
conn = CommonDB.connectGP(configFile);
startAll(conn);
conn.close();
}
}
catch (SQLException ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
finally
{
if (conn != null)
conn.close();
}
}
private static void startAll(Connection conn) throws SQLException
{
String id = "";
String tableName = "";
String columns = "";
String columnDataTypes = "";
String sqlText = "";
String sourceType = "";
String sourceServerName = "";
String sourceInstanceName = "";
String sourcePort = "";
String sourceDatabaseName = "";
String sourceUserName = "";
String sourcePass = "";
int gpfdistPort = 0;
int columnCount = 0;
try
{
String strSQL = "SELECT id, table_name, columns, column_datatypes, sql_text,\n";
strSQL += "source_type, source_server_name, source_instance_name, source_port, source_database_name, source_user_name, source_pass,\n";
strSQL += "array_upper(columns, 1) as column_count\n";
strSQL += "FROM os.custom_sql";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(strSQL);
while (rs.next())
{
id = rs.getString(1);
tableName = rs.getString(2);
columns = rs.getString(3);
columnDataTypes = 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);
columnCount = rs.getInt(13);
if (sourceType == null)
sourceType = "";
if (sourceInstanceName == null)
sourceInstanceName = "";
if (sourcePort == null)
sourcePort = "";
if (sourceDatabaseName == null)
sourceDatabaseName = "";
updateTable(conn, id, tableName, columns, columnDataTypes, sqlText, sourceType, sourceServerName, sourceInstanceName, sourcePort, sourceDatabaseName, sourceUserName, sourcePass);
}
}
catch (SQLException ex)
{
throw new SQLException(ex.getMessage());
}
}
private static void updateTable(Connection conn, String id, String tableName, String columns, String columnDataTypes, String sqlText, String sourceType, String sourceServerName, String sourceInstanceName, String sourcePort, String sourceDatabaseName, String sourceUserName, String sourcePass) throws SQLException
{
columns = "'" + columns + "'";
columnDataTypes = "'" + columnDataTypes + "'";
sqlText = setSQLString(sqlText);
sourceType = setSQLString(sourceType);
sourceServerName = setSQLString(sourceServerName);
sourceInstanceName = setSQLString(sourceInstanceName);
sourcePort = setSQLInt(sourcePort);
sourceDatabaseName = setSQLString(sourceDatabaseName);
sourceUserName = setSQLString(sourceUserName);
sourcePass = setSQLString(sourcePass);
try
{
//stop the gpfdist process for this job
customStop(conn, id);
//drop the old version of the table
tableName = tableName.toLowerCase();
dropExtTable(conn, tableName);
//add the quotes for the table name so the insert works properly
tableName = setSQLString(tableName);
//dynamically get the gpfdist port for the custom table
int gpfdistPort = GpfdistRunner.customStart(OSProperties.osHome);
System.out.println("Starting gpfdist on port " + gpfdistPort);
//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 += " " + columns + ", \n";
strSQL += " " + columnDataTypes + ", \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";
updateTable(conn, strSQL);
//create the external table
createExtTable(conn, id);
}
catch (SQLException ex)
{
throw new SQLException(ex.getMessage());
}
}
private static String setSQLString(String columnValue)
{
columnValue = columnValue.replace("'", "\\'");
if (columnValue.equals(""))
columnValue = "null";
else
columnValue = "'" + columnValue + "'";
return columnValue;
}
private static String setSQLInt(String columnValue)
{
if (columnValue.equals(""))
columnValue = "null";
return columnValue;
}
private static void updateTable(Connection conn, String strSQL) throws SQLException
{
try
{
Statement stmt = conn.createStatement();
stmt.executeUpdate(strSQL);
}
catch (SQLException ex)
{
throw new SQLException(ex.getMessage());
}
}
private static void customStop(Connection conn, 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)";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(strSQL);
while (rs.next())
{
myGpfdistPort = rs.getInt(1);
GpfdistRunner.customStop(OSProperties.osHome, myGpfdistPort);
}
}
catch (SQLException ex)
{
throw new SQLException(ex.getMessage());
}
}
private static void dropExtTable(Connection conn, String tableName) throws SQLException
{
try
{
String strSQL = "DROP EXTERNAL TABLE IF EXISTS " + tableName;
updateTable(conn, strSQL);
}
catch (SQLException ex)
{
throw new SQLException(ex.getMessage());
}
}
private static void createExtTable(Connection conn, 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;
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(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')";
updateTable(conn, strSQLCreateTable);
}
catch (SQLException ex)
{
throw new SQLException(ex.getMessage());
}
}
}