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