1ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5ef39d40dSAndrew Rist  * distributed with this work for additional information
6ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10ef39d40dSAndrew Rist  *
11ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12ef39d40dSAndrew Rist  *
13ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18ef39d40dSAndrew Rist  * under the License.
19ef39d40dSAndrew Rist  *
20ef39d40dSAndrew Rist  *************************************************************/
21ef39d40dSAndrew Rist 
22ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package convwatch;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import java.sql.Connection;
27cdf0e10cSrcweir import java.sql.DriverManager;
28cdf0e10cSrcweir import java.sql.Statement;
29cdf0e10cSrcweir import java.sql.ResultSet;
30cdf0e10cSrcweir import java.sql.SQLException;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir import java.lang.Thread;
33cdf0e10cSrcweir import java.util.StringTokenizer;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir class ShareConnection
36cdf0e10cSrcweir {
37cdf0e10cSrcweir     private Connection m_aConnection = null;
ShareConnection()38cdf0e10cSrcweir     public ShareConnection()
39cdf0e10cSrcweir         {}
40cdf0e10cSrcweir 
getConnection()41cdf0e10cSrcweir     public Connection getConnection()
42cdf0e10cSrcweir         {
43cdf0e10cSrcweir             if (m_aConnection == null)
44cdf0e10cSrcweir             {
45cdf0e10cSrcweir                 try
46cdf0e10cSrcweir                 {
47cdf0e10cSrcweir                     m_aConnection = DBHelper.getMySQLConnection();
48cdf0e10cSrcweir                 }
49cdf0e10cSrcweir                 catch(java.sql.SQLException e)
50cdf0e10cSrcweir                 {
51cdf0e10cSrcweir                     GlobalLogWriter.get().println("DB: ERROR: can't connect to DB.");
52cdf0e10cSrcweir                     m_aConnection = null;
53cdf0e10cSrcweir                 }
54cdf0e10cSrcweir             }
55cdf0e10cSrcweir             return m_aConnection;
56cdf0e10cSrcweir         }
57cdf0e10cSrcweir }
58cdf0e10cSrcweir 
59cdf0e10cSrcweir     class MySQLThread extends Thread
60cdf0e10cSrcweir     {
61cdf0e10cSrcweir         Connection m_aCon = null;
62cdf0e10cSrcweir         String m_sSQL;
MySQLThread(Connection _aCon, String _sSQL)63cdf0e10cSrcweir         public MySQLThread(Connection _aCon, String _sSQL)
64cdf0e10cSrcweir             {
65cdf0e10cSrcweir                 m_aCon = _aCon;
66cdf0e10cSrcweir                 m_sSQL = _sSQL;
67cdf0e10cSrcweir             }
68cdf0e10cSrcweir 
run()69cdf0e10cSrcweir         public void run()
70cdf0e10cSrcweir             {
71cdf0e10cSrcweir                 Statement oStmt = null;
72cdf0e10cSrcweir                 if (m_aCon == null)
73cdf0e10cSrcweir                 {
74cdf0e10cSrcweir                     GlobalLogWriter.get().println("DB: ERROR: in ExecSQL, connection not established.");
75cdf0e10cSrcweir                     return;
76cdf0e10cSrcweir                 }
77cdf0e10cSrcweir 
78cdf0e10cSrcweir                 // Connection oCon = null;
79cdf0e10cSrcweir                 try
80cdf0e10cSrcweir                 {
81cdf0e10cSrcweir                     // oCon = getMySQLConnection();
82cdf0e10cSrcweir                     oStmt = m_aCon.createStatement();
83cdf0e10cSrcweir 
84cdf0e10cSrcweir                     GlobalLogWriter.get().println("DB: " + m_sSQL);
85cdf0e10cSrcweir                     /* ResultSet oResult = */
86cdf0e10cSrcweir                     oStmt.executeUpdate(m_sSQL);
87cdf0e10cSrcweir                 }
88cdf0e10cSrcweir                 catch(Exception e)
89cdf0e10cSrcweir                 {
90cdf0e10cSrcweir                     GlobalLogWriter.get().println("DB: Couldn't execute sql string '" + m_sSQL + "'");
91cdf0e10cSrcweir                     GlobalLogWriter.get().println("DB: Reason: " + e.getMessage());
92cdf0e10cSrcweir                 }
93cdf0e10cSrcweir             }
94cdf0e10cSrcweir     }
95cdf0e10cSrcweir 
96cdf0e10cSrcweir public class DBHelper
97cdf0e10cSrcweir {
98cdf0e10cSrcweir     /**
99cdf0e10cSrcweir      * This method inserts given values into<br>
100cdf0e10cSrcweir      * the table 'states'
101cdf0e10cSrcweir      * @param values a set of comma separated values to be inserted
102cdf0e10cSrcweir      */
103cdf0e10cSrcweir 
SQLinsertValues(Connection _aCon, String _sTableName, String value_names, String values)104cdf0e10cSrcweir     public void SQLinsertValues(Connection _aCon, String _sTableName, String value_names, String values)
105cdf0e10cSrcweir         {
106cdf0e10cSrcweir             if (_aCon == null)
107cdf0e10cSrcweir             {
108cdf0e10cSrcweir                 GlobalLogWriter.get().println("DB: ERROR: in SQLinsertValues, connection not established.");
109cdf0e10cSrcweir                 return;
110cdf0e10cSrcweir             }
111cdf0e10cSrcweir 
112cdf0e10cSrcweir             // String aInsertStr = "";
113cdf0e10cSrcweir             //
114cdf0e10cSrcweir             // aInsertStr = "INSERT INTO " + _sTableName + " (" + value_names + " ) VALUES (" + values + ")";
115cdf0e10cSrcweir             // ExecSQL(_aCon, aInsertStr);
116cdf0e10cSrcweir             StringBuffer aInsertStr = new StringBuffer();
117cdf0e10cSrcweir 
118cdf0e10cSrcweir             aInsertStr.append( "INSERT INTO " ) . append( _sTableName );
119cdf0e10cSrcweir             aInsertStr.append( " (").append( value_names ).append ( ")" );
120cdf0e10cSrcweir             aInsertStr.append(" VALUES (" ).append( values ).append( ")" );
121cdf0e10cSrcweir             ExecSQL(_aCon, aInsertStr.toString() );
122cdf0e10cSrcweir         }
123cdf0e10cSrcweir 
SQLupdateValue(Connection _aCon, String _sTableName, String _sSet, String _sWhere)124cdf0e10cSrcweir     public void SQLupdateValue(Connection _aCon, String _sTableName, String _sSet, String _sWhere)
125cdf0e10cSrcweir         {
126cdf0e10cSrcweir             if (_aCon == null)
127cdf0e10cSrcweir             {
128cdf0e10cSrcweir                 GlobalLogWriter.get().println("DB: ERROR: in SQLinsertValues, connection not established.");
129cdf0e10cSrcweir                 return;
130cdf0e10cSrcweir             }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir             // String aUpdateStr = "";
133cdf0e10cSrcweir             //
134cdf0e10cSrcweir             // aUpdateStr = "UPDATE " + _sTableName + " SET " + _sSet + " WHERE " + _sWhere;
135cdf0e10cSrcweir             // ExecSQL( _aCon, aUpdateStr );
136cdf0e10cSrcweir             StringBuffer aUpdateStr = new StringBuffer();
137cdf0e10cSrcweir 
138cdf0e10cSrcweir             aUpdateStr.append( "UPDATE " ).append( _sTableName )
139cdf0e10cSrcweir                 .append( " SET " ).append( _sSet )
140cdf0e10cSrcweir                 .append( " WHERE " ).append( _sWhere );
141cdf0e10cSrcweir             ExecSQL( _aCon, aUpdateStr.toString() );
142cdf0e10cSrcweir         }
143cdf0e10cSrcweir 
144cdf0e10cSrcweir     private static String m_sDBServerName;
145cdf0e10cSrcweir     private static String m_sDBName;
146cdf0e10cSrcweir     private static String m_sDBUser;
147cdf0e10cSrcweir     private static String m_sDBPasswd;
148cdf0e10cSrcweir 
fillDBConnection(String _sInfo)149cdf0e10cSrcweir     protected synchronized void fillDBConnection(String _sInfo)
150cdf0e10cSrcweir         {
151cdf0e10cSrcweir             StringTokenizer aTokenizer = new StringTokenizer(_sInfo,",",false);
152cdf0e10cSrcweir             while (aTokenizer.hasMoreTokens())
153cdf0e10cSrcweir             {
154cdf0e10cSrcweir                 String sPart = aTokenizer.nextToken();
155cdf0e10cSrcweir                 if (sPart.startsWith("db:"))
156cdf0e10cSrcweir                 {
157cdf0e10cSrcweir                     m_sDBName = sPart.substring(3);
158cdf0e10cSrcweir                     // GlobalLogWriter.get().println("DB: source version: " + m_sSourceVersion);
159cdf0e10cSrcweir                 }
160cdf0e10cSrcweir                 else if (sPart.startsWith("user:"))
161cdf0e10cSrcweir                 {
162cdf0e10cSrcweir                     m_sDBUser = sPart.substring(5);
163cdf0e10cSrcweir                 }
164cdf0e10cSrcweir                 else if (sPart.startsWith("passwd:"))
165cdf0e10cSrcweir                 {
166cdf0e10cSrcweir                     m_sDBPasswd = sPart.substring(7);
167cdf0e10cSrcweir                 }
168cdf0e10cSrcweir                 else if (sPart.startsWith("server:"))
169cdf0e10cSrcweir                 {
170cdf0e10cSrcweir                     m_sDBServerName = sPart.substring(7);
171cdf0e10cSrcweir                 }
172cdf0e10cSrcweir             }
173cdf0e10cSrcweir         }
174cdf0e10cSrcweir 
175cdf0e10cSrcweir     /**
176cdf0e10cSrcweir      * This method establishes a Connection<br>
177cdf0e10cSrcweir      * with the database 'module_unit' on jakobus
178cdf0e10cSrcweir      */
179cdf0e10cSrcweir 
getMySQLConnection()180cdf0e10cSrcweir     public static Connection getMySQLConnection() throws SQLException
181cdf0e10cSrcweir         {
182cdf0e10cSrcweir             try
183cdf0e10cSrcweir             {
184cdf0e10cSrcweir                 Class.forName("org.gjt.mm.mysql.Driver");
185cdf0e10cSrcweir                 String sConnection = "jdbc:mysql://" + m_sDBServerName + ":3306/" + m_sDBName;
186cdf0e10cSrcweir                 // Connection mysql = DriverManager.getConnection(
187cdf0e10cSrcweir                 //    "jdbc:mysql://jakobus:3306/jobs_convwatch","admin","admin");
188cdf0e10cSrcweir                 Connection mysql = DriverManager.getConnection(sConnection, m_sDBUser, m_sDBPasswd);
189cdf0e10cSrcweir                 return mysql;
190cdf0e10cSrcweir             }
191cdf0e10cSrcweir             catch (ClassNotFoundException e)
192cdf0e10cSrcweir             {
193cdf0e10cSrcweir                 GlobalLogWriter.get().println("DB: Class not found exception caught: " + e.getMessage());
194cdf0e10cSrcweir                 GlobalLogWriter.get().println("DB: Maybe mysql.jar is not added to the classpath.");
195cdf0e10cSrcweir             }
196cdf0e10cSrcweir             return null;
197cdf0e10cSrcweir         }
198cdf0e10cSrcweir 
199cdf0e10cSrcweir 
200cdf0e10cSrcweir     /**
201cdf0e10cSrcweir      * This method removes all entries of the given<br>
202cdf0e10cSrcweir      * module/platform combination
203cdf0e10cSrcweir      * @param mdl the name of the module, e.g. sal
204cdf0e10cSrcweir      * @param os the name of the platform, e.g. unxsols
205cdf0e10cSrcweir      */
206cdf0e10cSrcweir     // LLA: public static void SQLdeleteValues(Connection _aCon, String _sEnvironment, String _sUnitName, String _sMethodName, String _sCWS, String _sDate)
207cdf0e10cSrcweir     // LLA:     {
208cdf0e10cSrcweir     // LLA:         String sSQL =
209cdf0e10cSrcweir     // LLA:             "DELETE FROM states WHERE " +
210cdf0e10cSrcweir     // LLA:             "     unit=" + DatabaseEntry.Quote(_sUnitName) +
211cdf0e10cSrcweir     // LLA:             " AND pf="   + DatabaseEntry.Quote (_sEnvironment) +
212cdf0e10cSrcweir     // LLA:             " AND meth=" + DatabaseEntry.Quote (_sMethodName) +
213cdf0e10cSrcweir     // LLA:             " AND cws="  + DatabaseEntry.Quote(_sCWS) +
214cdf0e10cSrcweir     // LLA:             " AND dt="   + DatabaseEntry.Quote(_sDate);
215cdf0e10cSrcweir     // LLA:
216cdf0e10cSrcweir     // LLA:         // ExecSQL(_aCon, sSQL);
217cdf0e10cSrcweir     // LLA:     }
218cdf0e10cSrcweir 
ExecSQL(Connection _aCon, String _sSQL)219cdf0e10cSrcweir     protected synchronized void ExecSQL(Connection _aCon, String _sSQL)
220cdf0e10cSrcweir             {
221cdf0e10cSrcweir                 MySQLThread aSQLThread = new MySQLThread(_aCon, _sSQL);
222cdf0e10cSrcweir                 aSQLThread.start();
223cdf0e10cSrcweir             }
224cdf0e10cSrcweir 
225cdf0e10cSrcweir 
226cdf0e10cSrcweir 
227cdf0e10cSrcweir     // public static int QueryIntFromSQL(String _sSQL, String _sColumnName, String _sValue)
228cdf0e10cSrcweir     //     {
229cdf0e10cSrcweir     //         boolean bNeedSecondTry = false;
230cdf0e10cSrcweir     //         int nValue = 0;
231cdf0e10cSrcweir     //         do
232cdf0e10cSrcweir     //         {
233cdf0e10cSrcweir     //             try
234cdf0e10cSrcweir     //             {
235cdf0e10cSrcweir     //                 nValue = QueryIntFromSQL(_sSQL, _sColumnName, _sValue);
236cdf0e10cSrcweir     //             }
237cdf0e10cSrcweir     //             catch (ValueNotFoundException e)
238cdf0e10cSrcweir     //             {
239cdf0e10cSrcweir     //                 bNeedSecondTry = true;
240cdf0e10cSrcweir     //                 String sSQL = "INSERT INTO " + _sTable + "(" + _sColumnName + ") VALUES (" + _sValue + ")";
241cdf0e10cSrcweir     //                 ExecSQL(sSQL);
242cdf0e10cSrcweir     //             }
243cdf0e10cSrcweir     //         } while (bNeedSecondTry);
244cdf0e10cSrcweir     //         return nValue;
245cdf0e10cSrcweir     //     }
246cdf0e10cSrcweir 
QueryIntFromSQL(Connection _aCon, String _sSQL, String _sColumnName)247cdf0e10cSrcweir     public int QueryIntFromSQL(Connection _aCon, String _sSQL, String _sColumnName)
248cdf0e10cSrcweir         throws ValueNotFoundException
249cdf0e10cSrcweir         {
250cdf0e10cSrcweir             Statement oStmt = null;
251cdf0e10cSrcweir             Connection oCon = null;
252cdf0e10cSrcweir             int nValue = 0;
253cdf0e10cSrcweir             try
254cdf0e10cSrcweir             {
255cdf0e10cSrcweir                 // oCon = getMySQLConnection();
256cdf0e10cSrcweir                 oStmt = _aCon.createStatement();
257cdf0e10cSrcweir 
258cdf0e10cSrcweir                 ResultSet oResult = oStmt.executeQuery(_sSQL);
259cdf0e10cSrcweir                 oResult.next();
260cdf0e10cSrcweir 
261cdf0e10cSrcweir 				try
262cdf0e10cSrcweir                 {
263cdf0e10cSrcweir                     if (_sColumnName.length() == 0)
264cdf0e10cSrcweir                     {
265cdf0e10cSrcweir                         // take the first row value (started with 1)
266cdf0e10cSrcweir                         nValue = oResult.getInt(1);
267cdf0e10cSrcweir                     }
268cdf0e10cSrcweir                     else
269cdf0e10cSrcweir                     {
270cdf0e10cSrcweir                         nValue = oResult.getInt(_sColumnName);
271cdf0e10cSrcweir                     }
272cdf0e10cSrcweir     	            // System.out.println("value: " + String.valueOf(nValue));
273cdf0e10cSrcweir 				}
274cdf0e10cSrcweir 				catch (SQLException e)
275cdf0e10cSrcweir 				{
276cdf0e10cSrcweir 	                String sError = e.getMessage();
277cdf0e10cSrcweir     	            GlobalLogWriter.get().println("DB: Original SQL error: " + sError);
278*bb6af6bcSPedro Giffuni                     throw new ValueNotFoundException("Can't execute SQL: " + _sSQL);
279cdf0e10cSrcweir 				}
280cdf0e10cSrcweir             }
281cdf0e10cSrcweir             catch(SQLException e)
282cdf0e10cSrcweir             {
283cdf0e10cSrcweir                 String sError = e.getMessage();
284cdf0e10cSrcweir                 GlobalLogWriter.get().println("DB: Couldn't execute sql string " + _sSQL + "\n" + sError);
285cdf0e10cSrcweir             }
286cdf0e10cSrcweir             return nValue;
287cdf0e10cSrcweir         }
288cdf0e10cSrcweir 
Quote(String _sToQuote)289cdf0e10cSrcweir 	public String Quote(String _sToQuote)
290cdf0e10cSrcweir 		{
291cdf0e10cSrcweir             String ts = "'";
292cdf0e10cSrcweir             String ds = "\"";
293cdf0e10cSrcweir             int nQuote = _sToQuote.indexOf(ts);
294cdf0e10cSrcweir             if (nQuote >= 0)
295cdf0e10cSrcweir             {
296cdf0e10cSrcweir                 return ds + _sToQuote + ds;
297cdf0e10cSrcweir             }
298cdf0e10cSrcweir 			return ts + _sToQuote + ts;
299cdf0e10cSrcweir 		}
300cdf0e10cSrcweir 
301cdf0e10cSrcweir /* default date format in the MySQL DB yyyy-MM-dd */
today()302cdf0e10cSrcweir     public static String today()
303cdf0e10cSrcweir         {
304cdf0e10cSrcweir             return DateHelper.getDateString("yyyy-MM-dd");
305cdf0e10cSrcweir         }
306cdf0e10cSrcweir 
307cdf0e10cSrcweir     public static final String sComma = ",";
308cdf0e10cSrcweir     public static final String sEqual = "=";
309cdf0e10cSrcweir     public static final String sAND = " AND ";
310cdf0e10cSrcweir 
311cdf0e10cSrcweir }
312cdf0e10cSrcweir 
313