forked from RunningJon/outsourcer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OutsourcerModel.java
150 lines (130 loc) · 3.15 KB
/
OutsourcerModel.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
import java.sql.*;
import java.net.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
public class OutsourcerModel
{
public static ResultSet getResults(String strSQL) throws SQLException
{
try
{
Connection conn = UIConnectionFactory.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(strSQL);
conn.close();
return rs;
}
catch (SQLException ex)
{
throw new SQLException(ex.getMessage());
}
}
public static ArrayList<String> getStringArray(String strSQL) throws SQLException
{
ArrayList<String> stringArray = new ArrayList<String>();
try
{
String output = "";
String columnValue = "";
Connection conn = UIConnectionFactory.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(strSQL);
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
while (rs.next())
{
output = "";
for (int i=1; i<numberOfColumns+1; i++)
{
columnValue = rs.getString(i);
if (i == 1)
output = columnValue;
else
output += ';' + columnValue;
}
stringArray.add(output);
}
conn.close();
return stringArray;
}
catch (SQLException ex)
{
throw new SQLException(ex.getMessage());
}
}
public static ArrayList<String> gpArrayToJavaArray(String gpArray)
{
//Array in Greenplum is formatted as a string with { }
gpArray = gpArray.substring(1, gpArray.length() - 1);
ArrayList<String> stringArray = new ArrayList<String>(Arrays.asList(gpArray.split(",")));
return stringArray;
}
public static String javaArrayToString(ArrayList<String> stringArray)
{
int i = 0;
String output = "{";
for (String s : stringArray)
{
s = "'" + s.replaceAll("[^a-zA-Z0-9]", "") + "'";
s = s.toLowerCase();
i++;
if (i == 1)
output = "ARRAY[" + s;
else
output += "," + s;
}
output += "]";
return output;
}
public static void updateTable(String strSQL) throws SQLException
{
try
{
Connection conn = UIConnectionFactory.getConnection();
Statement stmt = conn.createStatement();
stmt.executeUpdate(strSQL);
conn.close();
}
catch (SQLException ex)
{
throw new SQLException(ex.getMessage());
}
}
public static String setSQLString(boolean columnValue)
{
String strColumnValue = "";
if (columnValue)
strColumnValue = "true";
else
strColumnValue = "false";
return strColumnValue;
}
public static String setSQLString(String columnValue)
{
columnValue = columnValue.replace("'", "\\'");
if (columnValue.equals(""))
columnValue = "null";
else
columnValue = "'" + columnValue + "'";
return columnValue;
}
public static String setSQLString(int columnValue)
{
String strColumnValue = Integer.toString(columnValue);
if (strColumnValue.equals(""))
strColumnValue = "null";
return strColumnValue;
}
public static String setSQLInt(String columnValue)
{
if (columnValue.equals(""))
columnValue = "null";
return columnValue;
}
public static String setSQLTimestamp(Timestamp columnValue)
{
String strColumnValue = columnValue.toString();
return strColumnValue;
}
}