| 227 |
dpurdie |
1 |
//==============================================================================
|
|
|
2 |
//
|
|
|
3 |
// ERG TRANSIT SYSTEMS Licensed software
|
|
|
4 |
// (C) 2006 All rights reserved
|
|
|
5 |
//
|
|
|
6 |
//==============================================================================
|
|
|
7 |
//
|
|
|
8 |
// Description: This program will take an SQL connection information from its
|
|
|
9 |
// commad line arguments.
|
|
|
10 |
//
|
|
|
11 |
// The program will listen on stdin for an SQL statement. It will
|
|
|
12 |
// then prepare and execute the statement.
|
|
|
13 |
//
|
|
|
14 |
// The results are streamed out of the program on STDOUT
|
|
|
15 |
//
|
|
|
16 |
// The program is intended to assist in replacing the DBI::ODBC
|
|
|
17 |
// packages within the JATS deployments scripts.
|
|
|
18 |
//
|
|
|
19 |
//
|
|
|
20 |
// Inputs:
|
|
|
21 |
// Arguments:
|
|
|
22 |
// url - Fed into the getConnection
|
|
|
23 |
// user - Fed into the getConnection
|
|
|
24 |
// password - Fed into the getConnection
|
|
|
25 |
// StdIn:
|
|
|
26 |
// query - An SQL query
|
|
|
27 |
//
|
|
|
28 |
// Output: Data is streamed out sdtout. It is intended to processed by
|
|
|
29 |
// another program, and not a human.
|
|
|
30 |
// Each line constits of a a tag and a data field. The tag describes
|
|
|
31 |
// the data that follows and the progress of the operation.
|
|
|
32 |
//
|
|
|
33 |
// Output lines:
|
|
|
34 |
// Status:xxxxx - Indicates program status
|
|
|
35 |
// Error:xxxxx - Error messages
|
|
|
36 |
// Info:ColumnCount - SQL info: number of Cols of data
|
|
|
37 |
// DataStart: - Start of SQL response
|
|
|
38 |
// Data:xxxx,yyyy,zzzz,... - SQL response
|
|
|
39 |
// DataEnd: - End of SQL response
|
|
|
40 |
//
|
|
|
41 |
// The Data: lines contain coma-seperated fields. The fields are
|
|
|
42 |
// encoded as 4 bits per byte added to an ascii-0. A cheap
|
|
|
43 |
// hex encoding.
|
|
|
44 |
//
|
|
|
45 |
// Program exist code: 0: Sucess
|
|
|
46 |
// 1: Error encountered
|
|
|
47 |
//
|
|
|
48 |
//==============================================================================
|
|
|
49 |
|
|
|
50 |
package com.erggroup.jats;
|
|
|
51 |
import java.io.IOException;
|
|
|
52 |
import java.io.InputStream;
|
|
|
53 |
import java.io.InputStreamReader;
|
|
|
54 |
import java.io.BufferedReader;
|
|
|
55 |
import java.net.HttpURLConnection;
|
|
|
56 |
import java.net.MalformedURLException;
|
|
|
57 |
import java.net.URL;
|
|
|
58 |
import java.sql.CallableStatement;
|
|
|
59 |
import java.sql.Connection;
|
|
|
60 |
import java.sql.DriverManager;
|
|
|
61 |
import java.sql.SQLException;
|
|
|
62 |
import java.sql.Types;
|
|
|
63 |
import java.sql.ResultSet;
|
|
|
64 |
import java.sql.ResultSetMetaData;
|
|
|
65 |
import oracle.jdbc.driver.OracleDriver;
|
|
|
66 |
import com.erggroup.jats.TimedBufferedReader;
|
|
|
67 |
|
|
|
68 |
public class ReleaseManagerSelect
|
|
|
69 |
{
|
|
|
70 |
public static void main(String[] args)
|
|
|
71 |
{
|
|
|
72 |
int EndSelect = 0;
|
|
|
73 |
|
|
|
74 |
System.out.println( "Status:Program startup" );
|
|
|
75 |
|
|
|
76 |
if ( args.length != 3 )
|
|
|
77 |
{
|
|
|
78 |
System.out.println( "Error: Not enough arguments. Need 3, Have " + args.length );
|
|
|
79 |
System.exit( 1 );
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
//
|
|
|
83 |
// Extract user parameters
|
|
|
84 |
//
|
|
|
85 |
String url = args[0];
|
|
|
86 |
String user = args[1];
|
|
|
87 |
String passwd = args[2];
|
|
|
88 |
|
|
|
89 |
System.out.println( "Status:url:" + url );
|
|
|
90 |
System.out.println( "Status:user:" + user );
|
|
|
91 |
System.out.println( "Status:passwd:" + passwd );
|
|
|
92 |
|
|
|
93 |
|
|
|
94 |
//
|
|
|
95 |
// Use a read routine that will timeout if no activity occurs
|
|
|
96 |
// the process will exit, otherwise we appear to get stray instances
|
|
|
97 |
// of java running - if the user control-C while running.
|
|
|
98 |
//
|
|
|
99 |
BufferedReader br = new TimedBufferedReader(new InputStreamReader(System.in));
|
|
|
100 |
String query = null;
|
|
|
101 |
|
|
|
102 |
//
|
|
|
103 |
// Initiate the connection
|
|
|
104 |
//
|
|
|
105 |
try
|
|
|
106 |
{
|
|
|
107 |
DriverManager.registerDriver( new OracleDriver() );
|
|
|
108 |
System.out.println( "Status:Driver Opened" );
|
|
|
109 |
|
|
|
110 |
Connection conn = DriverManager.getConnection( url, user, passwd);
|
|
|
111 |
System.out.println( "Status:Connection Opened" );
|
|
|
112 |
System.out.println( "ConnectionOpened:" );
|
|
|
113 |
|
|
|
114 |
//
|
|
|
115 |
// Process requests
|
|
|
116 |
//
|
|
|
117 |
while ( EndSelect == 0 )
|
|
|
118 |
{
|
|
|
119 |
//
|
|
|
120 |
// Read in the next select request from the input stream
|
|
|
121 |
// Only SELECT statements will do something useful, but this could
|
|
|
122 |
// be extended.
|
|
|
123 |
//
|
|
|
124 |
System.out.println( "Status:ReadyForQuery");
|
|
|
125 |
try {
|
|
|
126 |
query = br.readLine();
|
|
|
127 |
} catch (IOException ioe) {
|
|
|
128 |
System.out.println("Error:IO error trying to read input");
|
|
|
129 |
System.exit(1);
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
if ( query.length() <= 0 )
|
|
|
133 |
{
|
|
|
134 |
System.out.println( "Status:Zero length query" );
|
|
|
135 |
EndSelect = 1;
|
|
|
136 |
continue;
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
//
|
|
|
140 |
// Determine the correct operation
|
|
|
141 |
// SELECT
|
|
|
142 |
// BEGIN
|
|
|
143 |
//
|
|
|
144 |
String begin = query.toUpperCase();
|
|
|
145 |
if ( begin.startsWith("SELECT") )
|
|
|
146 |
{
|
|
|
147 |
do_select( conn, query );
|
|
|
148 |
}
|
|
|
149 |
else
|
|
|
150 |
{
|
|
|
151 |
do_begin( conn, query );
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
//
|
|
|
155 |
// Ensure end of data is seen, even if an error is encountered
|
|
|
156 |
//
|
|
|
157 |
System.out.println( "DataEnd:");
|
|
|
158 |
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
conn.close();
|
|
|
162 |
System.out.println( "Status:ConnectionClosed" );
|
|
|
163 |
System.exit( 0 );
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
catch( SQLException e )
|
|
|
167 |
{
|
|
|
168 |
System.out.println( "Error:main caught java.sql.SQLException " );
|
|
|
169 |
System.out.println( "Error:" + e.getMessage().replace('\n', '~' ));
|
|
|
170 |
System.out.println( "Status:ConnectionClosed" );
|
|
|
171 |
System.exit( 1 );
|
|
|
172 |
}
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
//----------------------------------------------------------------------------
|
|
|
176 |
// FUNCTION : do_begin
|
|
|
177 |
//
|
|
|
178 |
// DESCRIPTION : Perform a SELECT operation
|
|
|
179 |
//
|
|
|
180 |
//
|
|
|
181 |
// INPUTS : conn - The connection
|
|
|
182 |
// query - The query
|
|
|
183 |
//
|
|
|
184 |
// RETURNS : nothing
|
|
|
185 |
//
|
|
|
186 |
|
|
|
187 |
public static void do_begin( Connection conn, String query )
|
|
|
188 |
{
|
|
|
189 |
try
|
|
|
190 |
{
|
|
|
191 |
System.out.println( "Status:begin:" + query );
|
|
|
192 |
CallableStatement stmt = conn.prepareCall( query );
|
|
|
193 |
stmt.executeUpdate();
|
|
|
194 |
stmt.close();
|
|
|
195 |
System.out.println( "DataStart:");
|
|
|
196 |
|
|
|
197 |
}
|
|
|
198 |
catch( SQLException e )
|
|
|
199 |
{
|
|
|
200 |
System.out.println( "Warning:main caught java.sql.SQLException " );
|
|
|
201 |
System.out.println( "Warning:" + e.getMessage().replace('\n', '~' ));
|
|
|
202 |
}
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
//----------------------------------------------------------------------------
|
|
|
206 |
// FUNCTION : do_select
|
|
|
207 |
//
|
|
|
208 |
// DESCRIPTION : Perform a SELECT operation
|
|
|
209 |
//
|
|
|
210 |
//
|
|
|
211 |
// INPUTS : conn - The connection
|
|
|
212 |
// query - The query
|
|
|
213 |
//
|
|
|
214 |
// RETURNS : nothing
|
|
|
215 |
//
|
|
|
216 |
public static void do_select( Connection conn, String query )
|
|
|
217 |
{
|
|
|
218 |
System.out.println( "Status:query:" + query );
|
|
|
219 |
|
|
|
220 |
|
|
|
221 |
//
|
|
|
222 |
// Execute the query and return the results to the user
|
|
|
223 |
// All the results are streamed to the user in one block
|
|
|
224 |
// It is not possible to interleave requests and fetches at this
|
|
|
225 |
// level. That should be done by the consiming wrapper.
|
|
|
226 |
//
|
|
|
227 |
try {
|
|
|
228 |
CallableStatement stmt = conn.prepareCall( query );
|
|
|
229 |
System.out.println( "Status:Statement prepared" );
|
|
|
230 |
|
|
|
231 |
ResultSet rset = stmt.executeQuery();
|
|
|
232 |
System.out.println( "Status:Query Done" );
|
|
|
233 |
|
|
|
234 |
//
|
|
|
235 |
// Determine the numnber of columns
|
|
|
236 |
//
|
|
|
237 |
ResultSetMetaData rsmd = rset.getMetaData();
|
|
|
238 |
int numberOfColumns = rsmd.getColumnCount();
|
|
|
239 |
System.out.println( "Info:ColumnCount:" + numberOfColumns );
|
|
|
240 |
|
|
|
241 |
//
|
|
|
242 |
// Return all rows to the user
|
|
|
243 |
//
|
|
|
244 |
System.out.println( "DataStart:");
|
|
|
245 |
while (rset.next ())
|
|
|
246 |
{
|
|
|
247 |
for ( int ii = 1; ii <= numberOfColumns; ii++ )
|
|
|
248 |
{
|
|
|
249 |
if ( ii != 1 )
|
|
|
250 |
System.out.print( "," );
|
|
|
251 |
else
|
|
|
252 |
System.out.print( "Data:" );
|
|
|
253 |
|
|
|
254 |
|
|
|
255 |
String s = rset.getString(ii);
|
|
|
256 |
StringBuffer sb = new StringBuffer (200);
|
|
|
257 |
|
|
|
258 |
//
|
|
|
259 |
// We have the data as text, but it may have characters in it
|
|
|
260 |
// that we can't pass through to our user. Need to esacpe
|
|
|
261 |
// returns
|
|
|
262 |
// new lines
|
|
|
263 |
// commas
|
|
|
264 |
//
|
|
|
265 |
// Convert all data to text bytes. Not quite HEX. Use 0123456789:;<=>?
|
|
|
266 |
// Simple and rude way to ensure that all data gets through
|
|
|
267 |
//
|
|
|
268 |
if ( s != null )
|
|
|
269 |
{
|
|
|
270 |
for (int i = 0; i < s.length (); i++)
|
|
|
271 |
{
|
|
|
272 |
char c = s.charAt (i); // Extract character
|
|
|
273 |
char c1 = (char)(c & 0x0f);
|
|
|
274 |
char c2 = (char)((c >> 4) & 0x0f);
|
|
|
275 |
|
|
|
276 |
sb.append( (char)(c2 + '0') );
|
|
|
277 |
sb.append( (char)(c1 + '0') );
|
|
|
278 |
}
|
|
|
279 |
}
|
|
|
280 |
System.out.print( sb );
|
|
|
281 |
}
|
|
|
282 |
System.out.println();
|
|
|
283 |
}
|
|
|
284 |
|
|
|
285 |
//
|
|
|
286 |
// Release resources
|
|
|
287 |
//
|
|
|
288 |
rset.close();
|
|
|
289 |
stmt.close();
|
|
|
290 |
|
|
|
291 |
}
|
|
|
292 |
catch( SQLException e )
|
|
|
293 |
{
|
|
|
294 |
System.out.println( "Warning:main caught java.sql.SQLException " );
|
|
|
295 |
System.out.println( "Warning:" + e.getMessage().replace('\n', '~' ));
|
|
|
296 |
}
|
|
|
297 |
}
|
|
|
298 |
}
|