KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > apps > form > VSQLProcess


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2003 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.apps.form;
15
16 import java.awt.*;
17 import java.awt.event.*;
18
19 import javax.swing.*;
20 import java.sql.*;
21 import java.util.StringTokenizer JavaDoc;
22
23 import org.compiere.apps.*;
24 import org.compiere.grid.ed.*;
25 import org.compiere.swing.*;
26 import org.compiere.util.*;
27
28 /**
29  * Process SQL Commands
30  *
31  * @author Jorg Janke
32  * @version $Id: VSQLProcess.java,v 1.1 2003/10/04 23:40:54 jjanke Exp $
33  */

34 public class VSQLProcess extends JPanel
35     implements FormPanel, ActionListener
36 {
37     /**
38      * Constructor
39      */

40     public VSQLProcess()
41     {
42     } // VSQLProcess
43

44     /**
45      * Initialize Panel
46      * @param WindowNo window
47      * @param frame frame
48      */

49     public void init (int WindowNo, FormFrame frame)
50     {
51         Log.trace(Log.l1_User, "VSQLProcess.init");
52         m_WindowNo = WindowNo;
53         m_frame = frame;
54         try
55         {
56             jbInit();
57             frame.getContentPane().add(this, BorderLayout.CENTER);
58         // frame.getContentPane().add(confirmPanel, BorderLayout.SOUTH);
59
}
60         catch(Exception JavaDoc e)
61         {
62             Log.error("VSQLProcess.init", e);
63         }
64     } // init
65

66     /** Window No */
67     private int m_WindowNo = 0;
68     /** FormFrame */
69     private FormFrame m_frame;
70
71     /** Log */
72     private static Logger s_log = Logger.getCLogger(VSQLProcess.class);
73
74     /** DML Statement */
75     private static final String JavaDoc[] DML_KEYWORDS = new String JavaDoc[]{
76         "SELECT", "UPDATE", "DELETE", "TRUNCATE"
77     };
78
79     private BorderLayout mainLayout = new BorderLayout();
80     private CPanel northPanel = new CPanel();
81     private CLabel sqlLabel = new CLabel("SQL");
82     private VText sqlField = new VText("SQL", false, false, true, 3000, 9000);
83     private JPanel centerPanel = new JPanel();
84     private BorderLayout centerLayout = new BorderLayout();
85     private BorderLayout northLayout = new BorderLayout();
86     private CText resultField = new CText(20,60);
87     private CButton sqlButton = ConfirmPanel.createProcessButton(true);
88
89     /**
90      * Static Init
91      * @throws Exception
92      */

93     void jbInit() throws Exception JavaDoc
94     {
95         this.setLayout(mainLayout);
96         mainLayout.setHgap(5);
97         mainLayout.setVgap(5);
98         //
99
this.add(northPanel, BorderLayout.NORTH);
100         northLayout.setHgap(5);
101         northLayout.setVgap(5);
102         northPanel.setLayout(northLayout);
103         sqlLabel.setText("SQL");
104         northPanel.add(sqlLabel, BorderLayout.WEST);
105         //
106
northPanel.add(sqlField, BorderLayout.CENTER);
107         sqlButton.addActionListener(this);
108         northPanel.add(sqlButton, BorderLayout.EAST);
109         //
110
this.add(centerPanel, BorderLayout.CENTER);
111         centerPanel.setLayout(centerLayout);
112         centerLayout.setHgap(0);
113         resultField.setReadWrite(false);
114         centerPanel.add(resultField, BorderLayout.CENTER);
115     } // jbInit
116

117     /**
118      * Action Listener
119      * @param e event
120      */

121     public void actionPerformed(ActionEvent e)
122     {
123         resultField.setText(processStatements (sqlField.getText(), false));
124     } // actionedPerformed
125

126     /**
127      * Process SQL Statements
128      * @param sqlStatements one or more statements separated by ;
129      * @param allowDML allow DML statements
130      * @return result
131      */

132     public static String JavaDoc processStatements (String JavaDoc sqlStatements, boolean allowDML)
133     {
134         if (sqlStatements == null || sqlStatements.length() == 0)
135             return "";
136         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
137         //
138
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(sqlStatements, ";", false);
139         while (st.hasMoreTokens())
140         {
141             result.append(processStatement(st.nextToken(), allowDML));
142             result.append(Env.NL);
143         }
144         //
145
return result.toString();
146     } // processStatements
147

148     /**
149      * Process SQL Statements
150      * @param sqlStatement one statement
151      * @param allowDML allow DML statements
152      * @return result
153      */

154     public static String JavaDoc processStatement (String JavaDoc sqlStatement, boolean allowDML)
155     {
156         if (sqlStatement == null)
157             return "";
158         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
159         char[] chars = sqlStatement.toCharArray();
160         for (int i = 0; i < chars.length; i++)
161         {
162             char c = chars[i];
163             if (Character.isWhitespace(c))
164                 sb.append(' ');
165             else
166                 sb.append(c);
167         }
168         String JavaDoc sql = sb.toString().trim();
169         if (sql.length() == 0)
170             return "";
171         //
172
StringBuffer JavaDoc result = new StringBuffer JavaDoc("SQL> ")
173             .append(sql)
174             .append(Env.NL);
175         if (!allowDML)
176         {
177             boolean error = false;
178             String JavaDoc SQL = sql.toUpperCase();
179             for (int i = 0; i < DML_KEYWORDS.length; i++)
180             {
181                 if (SQL.startsWith(DML_KEYWORDS[i] + " ")
182                     || SQL.indexOf(" " + DML_KEYWORDS[i] + " ") != -1
183                     || SQL.indexOf("(" + DML_KEYWORDS[i] + " ") != -1)
184                 {
185                     result.append("===> ERROR: Not Allowed Keyword ")
186                         .append(DML_KEYWORDS[i])
187                         .append(Env.NL);
188                     error = true;
189                 }
190             }
191             if (error)
192                 return result.toString();
193         } // !allowDML
194

195         // Process
196
Connection conn = DB.createConnection(true, Connection.TRANSACTION_READ_COMMITTED);
197         Statement stmt = null;
198         try
199         {
200             stmt = conn.createStatement();
201             boolean OK = stmt.execute(sql);
202             int count = stmt.getUpdateCount();
203             if (count == -1)
204             {
205                 result.append("---> ResultSet");
206             }
207             else
208                 result.append("---> Result=").append(count);
209         }
210         catch (SQLException e)
211         {
212             s_log.error("process statement: " + sql + " - " + e.toString());
213             result.append("===> ").append(e.toString());
214         }
215         
216         // Clean up
217
try
218         {
219             stmt.close();
220         }
221         catch (SQLException e1)
222         {
223             s_log.error("processStatement - close statement", e1);
224         }
225         stmt = null;
226         try
227         {
228             conn.close();
229         }
230         catch (SQLException e2)
231         {
232             s_log.error("processStatement - close connection", e2);
233         }
234         conn = null;
235         //
236
result.append(Env.NL);
237         return result.toString();
238     } // processStatement
239

240 } // VSQLProcess
241
Popular Tags