1 package com.explosion.datastream.exql.processes;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.sql.Connection;
23 import java.sql.SQLException;
24 import java.sql.Savepoint;
25
26 import org.apache.log4j.LogManager;
27 import org.apache.log4j.Logger;
28
29 import com.explosion.datastream.exql.gui.EXQLBaseTool;
30 import com.explosion.expfmodules.rdbmsconn.dbom.DataSet;
31 import com.explosion.expfmodules.rdbmsconn.dbom.utils.SQLEngine;
32 import com.explosion.expfmodules.rdbmsconn.dbom.utils.Transaction;
33 import com.explosion.utilities.exception.ExceptionManagerFactory;
34 import com.explosion.utilities.process.StackableSimpleProcess;
35 import com.explosion.utilities.process.threads.Process;
36 import com.explosion.utilities.process.threads.Finishable;
37
38 /***
39 * @author Stephen Cowx
40 * Date created:@14-Feb-2003
41 * This class executes a query and puts the results in a Dataset object
42 */
43 public class QueryProcess extends StackableSimpleProcess
44 {
45 private static Logger log = LogManager.getLogger(QueryProcess.class);
46 private Connection conn;
47 private String query;
48 private DataSet set;
49 private EXQLBaseTool tool;
50 private boolean fetchAllData = false;
51
52 /***
53 * Constructor for RefreshTopNodesProcess.
54 */
55 public QueryProcess(Finishable finishable, Connection conn, String query, boolean fetchAllData)
56 {
57 super(finishable,null);
58 this.conn = conn;
59 this.query = query;
60 this.setIsUserProcess(true);
61 }
62
63 /***
64 * Constructor for RefreshTopNodesProcess.
65 */
66 public QueryProcess(Finishable finishable, Connection conn, String query, boolean fetchAllData, EXQLBaseTool tool)
67 {
68 super(finishable,null);
69 this.conn = conn;
70 this.query = query;
71 this.tool = tool;
72 this.fetchAllData = fetchAllData;
73 this.setIsUserProcess(true);
74 }
75
76 /***
77 * Constructor for RefreshTopNodesProcess.
78 */
79 public QueryProcess(StackableSimpleProcess parentProcess, Finishable finishable, Connection conn, String query, boolean fetchAllData, EXQLBaseTool tool)
80 {
81 super(finishable, parentProcess);
82 this.conn = conn;
83 this.query = query;
84 this.tool = tool;
85 this.fetchAllData = fetchAllData;
86 this.setIsUserProcess(true);
87 }
88
89 public void process()
90 {
91 Transaction t = new Transaction(conn);
92
93 try
94 {
95 t.begin();
96
97 if (isStopped()) return;
98
99 setPercentComplete(10);
100
101 log(query);
102 SQLEngine engine = new SQLEngine();
103 Process process = null;
104
105
106 if (this.getProcessControl().getProcess() != null)
107 process = this.getProcessControl().getProcess();
108 else
109 process = this;
110
111 set = engine.executeCommand(conn, query, -1, process, fetchAllData);
112 engine.close();
113
114 if (set == null)
115 setStatusText("Done.");
116 else
117 setStatusText("Done. "+ set.getTableModel().getRowCount() + " rows returned.");
118
119 this.setPercentComplete(100);
120 log.debug(getStatusText());
121
122 t.commit();
123
124
125 Runtime.getRuntime().gc();
126 }
127 catch (Throwable e)
128 {
129 if (t!= null)
130 {
131 t.rollback();
132 }
133
134 if (e instanceof java.sql.SQLException && tool != null)
135 log(e,"Error: " + e.getMessage());
136 else
137 {
138 ExceptionManagerFactory.getExceptionManager().manageException( e, "Exception caught while executing SQL statement.");
139 }
140
141 setStatusText("Command not successful.");
142 setPercentComplete(100);
143 }
144
145 }
146
147 /***
148 * Returns the set.
149 * @return DataSet
150 */
151 public DataSet getSet()
152 {
153 return set;
154 }
155
156
157 public void log(String string)
158 {
159 tool.log(string);
160 }
161
162 public void log(Throwable exception, String message)
163 {
164 tool.log(message + exception.getMessage());
165 }
166
167 }