-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathParseScriptRunOneAtATime.java
More file actions
53 lines (42 loc) · 1.8 KB
/
ParseScriptRunOneAtATime.java
File metadata and controls
53 lines (42 loc) · 1.8 KB
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
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import oracle.dbtools.raptor.newscriptrunner.ISQLCommand;
import oracle.dbtools.raptor.newscriptrunner.ScriptParser;
import oracle.dbtools.raptor.newscriptrunner.ScriptRunner;
import oracle.dbtools.raptor.newscriptrunner.ScriptRunnerContext;
public class ParseScriptRunOneAtATime {
public static void main(String[] args) throws SQLException, IOException {
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/XE", "klrice", "klrice");
conn.setAutoCommit(false);
///Users/klrice/workspace_commons/sqlcl-java/
FileInputStream fin = new FileInputStream(new File("myfile.sql"));
ScriptParser parser = new ScriptParser(fin);
ISQLCommand cmd;
// #setup the context
ScriptRunnerContext ctx = new ScriptRunnerContext();
ctx.setBaseConnection(conn);
// Capture the results without this it goes to STDOUT
ByteArrayOutputStream bout = new ByteArrayOutputStream();
BufferedOutputStream buf = new BufferedOutputStream(bout);
ScriptRunner sr = new ScriptRunner(conn, buf, ctx);
while ( ( cmd = parser.next() ) != null ) {
// do something fancy based on a cmd
sr.run(cmd);
// check success/failure of the command
String errMsg = (String) ctx.getProperty(ScriptRunnerContext.ERR_MESSAGE);
if ( errMsg != null ){
// react to a failure
System.out.println("**FAILURE**" + errMsg);
}
}
String results = bout.toString("UTF8");
results = results.replaceAll(" force_print\n", "");
System.out.println(results);
}
}