forked from RunningJon/outsourcer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JobModel.java
298 lines (273 loc) · 12 KB
/
JobModel.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
import java.util.Map;
import java.sql.*;
import java.net.*;
import java.io.*;
import java.util.ArrayList;
public class JobModel
{
String id;
String refreshType;
String targetSchemaName;
String targetTableName;
boolean targetAppendOnly;
boolean targetCompressed;
boolean targetRowOrientation;
String sourceType;
String sourceServerName;
String sourceInstanceName;
String sourcePort;
String sourceDatabaseName;
String sourceSchemaName;
String sourceTableName;
String sourceUserName;
String sourcePass;
String columnName;
String sqlText;
boolean snapshot;
String scheduleDesc;
String scheduleNext;
public static ResultSet getList(String search, String limit, String offset, String sortBy, String sort) throws SQLException
{
String strSQL = "SELECT '<button onclick=\"updateJob(' || j.id || ', ''update'')\">Update</button>' ||\n";
strSQL += "' <button onclick=\"updateJob(' || j.id || ', ''delete'')\">Delete</button> ' || \n";
strSQL += "CASE WHEN q.status = 'processing' then '<button style=\"background-color:#707070\" onclick=\"void()\">Processing</button>'\n";
strSQL += " WHEN q.status = 'queued' then '<button style=\"background-color:#707070\" onclick=\"void()\">Queued</button>'\n";
strSQL += "ELSE\n";
strSQL += " '<button onclick=\"updateQueue(' || j.id || ', ''insert'')\">Queue</button>' END AS manage,\n";
strSQL += "j.id, initcap(j.refresh_type) AS refresh_type,\n";
strSQL += "CASE WHEN j.refresh_type = 'transform' THEN 'Transform' ELSE initcap(j.source_type) END AS source_info,\n";
strSQL += "CASE WHEN refresh_type = 'transform' THEN SUBSTRING(sql_text, 1, 30)\n";
strSQL += "ELSE COALESCE((j.target_schema_name || '.' || j.target_table_name), '') END AS target_table_name,\n";
strSQL += "COALESCE(j.schedule_desc, '') AS schedule_desc,\n";
strSQL += "COALESCE(schedule_next::text, '') AS schedule_next\n";
strSQL += "FROM os.job j\n";
strSQL += "LEFT OUTER JOIN (\n";
strSQL += "SELECT id, status FROM (\n";
strSQL += "SELECT id, status, row_number() over (partition by id order by queue_id desc) as rownum\n";
strSQL += "FROM os.queue) AS sub WHERE rownum = 1) AS q on j.id = q.id\n";
if (!search.equals(""))
{
strSQL += "WHERE LOWER(j.refresh_type) LIKE '%' || LOWER('" + search + "') || '%'\n";
strSQL += "OR LOWER(j.target_schema_name) LIKE '%' || LOWER('" + search + "') || '%'\n";
strSQL += "OR LOWER(j.target_table_name) LIKE '%' || LOWER('" + search + "') || '%'\n";
strSQL += "OR LOWER(j.source_type) LIKE '%' || LOWER('" + search + "') || '%'\n";
strSQL += "OR LOWER(j.source_server_name) LIKE '%' || LOWER('" + search + "') || '%'\n";
strSQL += "OR LOWER(j.source_instance_name) LIKE '%' || LOWER('" + search + "') || '%'\n";
strSQL += "OR LOWER(j.source_port) LIKE '%' || LOWER('" + search + "') || '%'\n";
strSQL += "OR LOWER(j.source_database_name) LIKE '%' || LOWER('" + search + "') || '%'\n";
strSQL += "OR LOWER(j.source_schema_name) LIKE '%' || LOWER('" + search + "') || '%'\n";
strSQL += "OR LOWER(j.source_table_name) LIKE '%' || LOWER('" + search + "') || '%'\n";
strSQL += "OR LOWER(j.source_user_name) LIKE '%' || LOWER('" + search +"') || '%'\n";
strSQL += "OR LOWER(j.column_name) LIKE '%' || LOWER('" + search + "') || '%'\n";
strSQL += "OR LOWER(j.sql_text) LIKE '%' || LOWER('" + search + "') || '%'\n";
}
sortBy = sortBy.toLowerCase();
if (sortBy.equals("id") || sortBy.equals("refresh_type") || sortBy.equals("source_info") || sortBy.equals("target_table_name") || sortBy.equals("schedule_desc") || sortBy.equals("schedule_next"))
strSQL += "ORDER BY " + sortBy + " " + sort + "\n";
else
strSQL += "ORDER BY j.id 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());
}
}
public static void insertTable(String id, String refreshType, String targetSchemaName, String targetTableName, boolean targetAppendOnly, boolean targetCompressed, boolean targetRowOrientation, String sourceType, String sourceServerName, String sourceInstanceName, String sourcePort, String sourceDatabaseName, String sourceSchemaName, String sourceTableName, String sourceUserName, String sourcePass, String columnName, String sqlText, boolean snapshot, String scheduleDesc) throws SQLException
{
refreshType = OutsourcerModel.setSQLString(refreshType);
targetSchemaName = OutsourcerModel.setSQLString(targetSchemaName);
targetTableName = OutsourcerModel.setSQLString(targetTableName);
String strTargetAppendOnly = OutsourcerModel.setSQLString(targetAppendOnly);
String strTargetCompressed = OutsourcerModel.setSQLString(targetCompressed);
String strTargetOrientation = OutsourcerModel.setSQLString(targetRowOrientation);
sourceType = OutsourcerModel.setSQLString(sourceType);
sourceServerName = OutsourcerModel.setSQLString(sourceServerName);
sourceInstanceName = OutsourcerModel.setSQLString(sourceInstanceName);
sourcePort = OutsourcerModel.setSQLInt(sourcePort);
sourceDatabaseName = OutsourcerModel.setSQLString(sourceDatabaseName);
sourceSchemaName = OutsourcerModel.setSQLString(sourceSchemaName);
sourceTableName = OutsourcerModel.setSQLString(sourceTableName);
sourceUserName = OutsourcerModel.setSQLString(sourceUserName);
sourcePass = OutsourcerModel.setSQLString(sourcePass);
columnName = OutsourcerModel.setSQLString(columnName);
sqlText = OutsourcerModel.setSQLString(sqlText);
String strSnapshot = OutsourcerModel.setSQLString(snapshot);
scheduleDesc = OutsourcerModel.setSQLString(scheduleDesc);
String strSQL = "INSERT INTO os.ao_job\n";
if (id.equals(""))
strSQL += "(";
else
strSQL += "(id, ";
strSQL += " refresh_type, target_schema_name, target_table_name,\n";
strSQL += " target_append_only, target_compressed, target_row_orientation,\n";
strSQL += " source_type, source_server_name, source_instance_name, source_port, source_database_name,\n";
strSQL += " source_schema_name, source_table_name, source_user_name, source_pass,\n";
strSQL += " column_name, sql_text, snapshot, schedule_desc)\n";
strSQL += "VALUES";
//if id is "", then this is a new job and the sequence will create a new id
if (id.equals(""))
strSQL += "(";
else
strSQL += "(" + id + ", ";
strSQL += " " + refreshType + ",\n";
strSQL += " " + targetSchemaName + ", " + targetTableName + ",\n";
strSQL += " " + strTargetAppendOnly + ", " + strTargetCompressed + ", " + targetRowOrientation + ",\n";
strSQL += " " + sourceType + ", " + sourceServerName + ", " + sourceInstanceName + ", " + sourcePort + ", " + sourceDatabaseName + ",\n";
strSQL += " " + sourceSchemaName + ", " + sourceTableName + ", " + sourceUserName + ", " + sourcePass + ",\n";
strSQL += " " + columnName + ",\n";
strSQL += " " + sqlText + ",\n";
strSQL += " " + strSnapshot + ",\n";
strSQL += " " + scheduleDesc + ")";
try
{
OutsourcerModel.updateTable(strSQL);
}
catch (SQLException ex)
{
throw new SQLException(ex.getMessage());
}
}
public static void deleteTable(String id) throws SQLException
{
String strSQL = "INSERT INTO os.ao_job\n";
strSQL += "(id, refresh_type, target_schema_name, target_table_name, target_append_only,\n";
strSQL += "target_compressed, target_row_orientation, source_type, source_server_name,\n";
strSQL += "source_instance_name, source_port, source_database_name, source_schema_name,\n";
strSQL += "source_table_name, source_user_name, source_pass, column_name,\n";
strSQL += "sql_text, snapshot, schedule_desc, schedule_next, schedule_change,\n";
strSQL += "deleted)\n";
strSQL += "SELECT id, refresh_type, target_schema_name, target_table_name, target_append_only,\n";
strSQL += "target_compressed, target_row_orientation, source_type, source_server_name,\n";
strSQL += "source_instance_name, source_port, source_database_name, source_schema_name,\n";
strSQL += "source_table_name, source_user_name, source_pass, column_name,\n";
strSQL += "sql_text, snapshot, schedule_desc, schedule_next, schedule_change,\n";
strSQL += "TRUE AS deleted\n";
strSQL += "FROM os.job\n";
strSQL += "WHERE id = " + id;
try
{
OutsourcerModel.updateTable(strSQL);
}
catch (SQLException ex)
{
throw new SQLException(ex.getMessage());
}
}
public JobModel (String aId) throws SQLException
{
String strSQL = "SELECT id, refresh_type, target_schema_name, target_table_name,\n";
strSQL += "target_append_only, target_compressed, target_row_orientation,\n";
strSQL += "source_type, source_server_name, source_instance_name, source_port, source_database_name,\n";
strSQL += "source_schema_name, source_table_name,\n";
strSQL += "source_user_name, source_pass,\n";
strSQL += "column_name, sql_text, snapshot, schedule_desc, schedule_next\n";
strSQL += "FROM os.job\n";
strSQL += "WHERE id = " + aId;
try
{
ResultSet rs = OutsourcerModel.getResults(strSQL);
while (rs.next())
{
id = Integer.toString(rs.getInt(1));
refreshType = rs.getString(2);
targetSchemaName = rs.getString(3);
targetTableName = rs.getString(4);
targetAppendOnly = rs.getBoolean(5);
targetCompressed = rs.getBoolean(6);
targetRowOrientation = rs.getBoolean(7);
sourceType = rs.getString(8);
sourceServerName = rs.getString(9);
sourceInstanceName = rs.getString(10);
sourcePort = rs.getString(11);
sourceDatabaseName = rs.getString(12);
sourceSchemaName = rs.getString(13);
sourceTableName = rs.getString(14);
sourceUserName = rs.getString(15);
sourcePass = rs.getString(16);
columnName = rs.getString(17);
sqlText = rs.getString(18);
snapshot = rs.getBoolean(19);
scheduleDesc = rs.getString(20);
scheduleNext = rs.getString(21);
}
}
catch (SQLException ex)
{
//do something??
}
}
public static JobModel getModel(String id)
{
try
{
return new JobModel(id);
}
catch (Exception ex)
{
return null;
}
}
public static ArrayList<String> getSchemas() throws SQLException
{
String strSQL = "SELECT target_schema_name AS schema_name\n";
strSQL += "FROM os.job\n";
strSQL += "GROUP BY target_schema_name\n";
strSQL += "ORDER BY schema_name";
ArrayList<String> schemas = new ArrayList<String>();
try
{
schemas = OutsourcerModel.getStringArray(strSQL);
return schemas;
}
catch (SQLException ex)
{
throw new SQLException(ex.getMessage());
}
}
public static void updateJobsSchedule(String scheduleDesc, String gpSchema) throws SQLException
{
scheduleDesc = OutsourcerModel.setSQLString(scheduleDesc);
gpSchema = OutsourcerModel.setSQLString(gpSchema);
try
{
String strSQL = "INSERT INTO os.ao_job\n";
strSQL += "(id, refresh_type, target_schema_name, target_table_name, target_append_only,\n";
strSQL += "target_compressed, target_row_orientation, source_type, source_server_name,\n";
strSQL += "source_instance_name, source_port, source_database_name, source_schema_name,\n";
strSQL += "source_table_name, source_user_name, source_pass, column_name,\n";
strSQL += "sql_text, snapshot, schedule_desc, schedule_next, schedule_change)\n";
strSQL += "SELECT id, refresh_type, target_schema_name, target_table_name, target_append_only,\n";
strSQL += "target_compressed, target_row_orientation, source_type, source_server_name,\n";
strSQL += "source_instance_name, source_port, source_database_name, source_schema_name,\n";
strSQL += "source_table_name, source_user_name, source_pass, column_name,\n";
strSQL += "sql_text, snapshot, " + scheduleDesc + " AS schedule_desc, schedule_next, TRUE as schedule_change\n";
strSQL += "FROM os.job\n";
strSQL += "WHERE target_schema_name = " + gpSchema;
OutsourcerModel.updateTable(strSQL);
}
catch (SQLException ex)
{
throw new SQLException(ex.getMessage());
}
}
public static void deleteTable() throws SQLException
{
String strSQL = "TRUNCATE os.ao_job";
try
{
OutsourcerModel.updateTable(strSQL);
}
catch (SQLException ex)
{
throw new SQLException(ex.getMessage());
}
}
}