001: /*
002: * SalomeTMF is a Test Management Framework
003: * Copyright (C) 2005 France Telecom R&D
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * @author Fayçal SOUGRATI
020: *
021: * Contact: mikael.marche@rd.francetelecom.com
022: */
023:
024: package org.objectweb.salome_tmf.api.api2db;
025:
026: import java.sql.Connection;
027: import java.sql.DatabaseMetaData;
028: import java.sql.DriverManager;
029: import java.sql.PreparedStatement;
030: import java.sql.ResultSetMetaData;
031: import java.sql.SQLException;
032: import java.sql.Statement;
033: import java.util.Vector;
034: import java.util.Hashtable;
035:
036: /**
037: * Classe encapsulant l'ensemble des objets necessaires e la connexion e la BdD
038: * SalomeTMF
039: */
040: public class DataBase {
041: /**
042: * Connexion e la BdD
043: */
044: Connection cnt = null;
045:
046: /**
047: * Objet contenant une table de la BdD ou le resultat d'une requete
048: */
049: DataSet results;
050:
051: /**
052: * Objet contenant des informations sur le nom et le type des colonnes du
053: * resultat d'une requete
054: */
055: ResultSetMetaData rsmd;
056:
057: /**
058: * Informations generales sur la BdD
059: */
060: DatabaseMetaData dmd;
061:
062: String[] types;
063:
064: /**
065: * Mapping des savapoint
066: */
067: Hashtable savepointMap;
068:
069: /**
070: * Constructeur
071: *
072: * @param driver
073: */
074:
075: //static Object pDriver = null;
076: Object pDriver = null;
077:
078: public DataBase(String driver) {
079: try {
080: savepointMap = new Hashtable();
081: types = new String[1];
082: types[0] = "TABLE";
083: //Class.forName(driver);
084: //org.objectweb.salome_tmf.api.Api.log("Driver use is : " + driver);
085: //if (pDriver == null) {
086: org.objectweb.salome_tmf.api.Api
087: .log("Creation du driver JDBC");
088: pDriver = Class.forName(driver).newInstance();
089: //}
090: //Class.forName("com.mysql.jdbc.Driver").newInstance();
091: } catch (Exception ex) {
092: org.objectweb.salome_tmf.api.Api
093: .log("Erreur lors du chargement du driver :"
094: + ex.getMessage());
095: }
096: }
097:
098: /**
099: * Fonction qui ouvre la connexion e la base de donnees
100: *
101: * @param url
102: * @param username
103: * @param password
104: */
105: public void open(String url, String username, String password) {
106: try {
107: org.objectweb.salome_tmf.api.Api.log("DB " + url
108: + ", user : " + username + "paswd " + password);
109: cnt = DriverManager.getConnection(url, username, password);
110: dmd = cnt.getMetaData();
111: results = new DataSet(dmd.getCatalogs());
112: cnt.setAutoCommit(false);
113: org.objectweb.salome_tmf.api.Api.log("Connection : " + cnt);
114:
115: } catch (SQLException ex) {
116: org.objectweb.salome_tmf.api.Api.log("Echec d'ouverture :"
117: + ex.getMessage());
118: }
119: }
120:
121: /**
122: * Fonction qui ouvre la connexion e la base de donnees
123: *
124: * @param url
125: * @param username
126: * @param password
127: */
128: public void open(String url, String username, String password,
129: java.util.Properties prop_proxy) {
130: try {
131: org.objectweb.salome_tmf.api.Api.log("DB " + url
132: + ", user : " + username + "paswd " + password);
133: if (prop_proxy != null) {
134: prop_proxy.put("user", username);
135: prop_proxy.put("password", password);
136: org.objectweb.salome_tmf.api.Api
137: .log("Connect to DB with properties");
138: cnt = DriverManager.getConnection(url, prop_proxy);
139: } else
140: cnt = DriverManager.getConnection(url, username,
141: password);
142: dmd = cnt.getMetaData();
143: results = new DataSet(dmd.getCatalogs());
144: cnt.setAutoCommit(false);
145: } catch (SQLException ex) {
146: org.objectweb.salome_tmf.api.Api.log("Echec d'ouverture :"
147: + ex.getMessage());
148: }
149: }
150:
151: /**
152: * Fonction qui ferme la connexion e la base de donnees
153: */
154: public void close() {
155: try {
156: cnt.close();
157: org.objectweb.salome_tmf.api.Api.log("Connection : " + cnt
158: + " is closed");
159: //cnt = null;
160: } catch (SQLException ex) {
161: org.objectweb.salome_tmf.api.Api
162: .log("Echec lors de la fermeture :"
163: + ex.getMessage());
164: }
165: }
166:
167: /**
168: * Fonction qui renvoie les noms des tables de la BdD
169: *
170: * @return
171: */
172: public String[] getTableNames() {
173: String[] tbnames = null;
174: Vector tname = new Vector();
175:
176: try {
177: results = new DataSet(dmd.getTables(null, null, "%", types));
178: while (results.hasMoreElements())
179: tname.addElement(results.getColumnValue("TABLE_NAME"));
180: } catch (SQLException ex) {
181: org.objectweb.salome_tmf.api.Api.log(ex.getMessage());
182: }
183: tbnames = new String[tname.size()];
184: for (int i = 0; i < tname.size(); i++)
185: tbnames[i] = (String) tname.elementAt(i);
186: return tbnames;
187: }
188:
189: /**
190: * Retourne les noms des tables de la BdD sous formes de chaines
191: *
192: * @return
193: */
194: public String[] getTableMetaData() {
195: results = null;
196:
197: try {
198: results = new DataSet(dmd.getTables(null, null, "%", types));
199: } catch (SQLException ex) {
200: org.objectweb.salome_tmf.api.Api.log(ex.getMessage());
201: }
202: return results.getMetaData();
203: }
204:
205: /**
206: * Retourne tous les champs de la table sous forme de chaines de caracteres
207: *
208: * @param tablename
209: * @return
210: */
211: public String[] getColumnMetaData(String tablename) {
212: results = null;
213:
214: try {
215: results = new DataSet(dmd.getColumns(null, null, tablename,
216: null));
217: } catch (SQLException ex) {
218: org.objectweb.salome_tmf.api.Api.log(ex.getMessage());
219: }
220: return results.getMetaData();
221: }
222:
223: /**
224: * Retourne les noms des colonnes de la table
225: *
226: * @param table
227: * @return
228: */
229: public String[] getColumnNames(String table) {
230: String[] tbnames = null;
231: Vector tname = new Vector();
232:
233: try {
234: results = new DataSet(dmd
235: .getTables(null, null, table, null));
236: while (results.hasMoreElements())
237: tname.addElement(results.getColumnValue("COLUMN_NAME"));
238: } catch (SQLException ex) {
239: org.objectweb.salome_tmf.api.Api.log(ex.getMessage());
240: }
241: tbnames = new String[tname.size()];
242: for (int i = 0; i < tname.size(); i++)
243: tbnames[i] = (String) tname.elementAt(i);
244: return tbnames;
245: }
246:
247: /**
248: * Donne la valeur d'un champ de la table en indiquant le nom de la colonne
249: *
250: * @param table
251: * @param columnName
252: */
253: public void getColumnValue(String table, String columnName) {
254: try {
255: if (table.length() > 0)
256: results = executeQuery("Select " + columnName
257: + " from " + table + " order by " + columnName);
258: } catch (Exception ex) {
259: org.objectweb.salome_tmf.api.Api
260: .log("Erreur sur la valeur de la colonne "
261: + columnName + ex.getMessage());
262: }
263: }
264:
265: /**
266: * Donne la valeur suivante pour la colonne passee en parametres
267: *
268: * @param columnName
269: * @return
270: */
271: public String getNextValue(String columnName) {
272: String res = "";
273: try {
274: if (results.hasMoreElements())
275: res = results.getColumnValue(columnName);
276: } catch (Exception ex) {
277: org.objectweb.salome_tmf.api.Api
278: .log("Erreur sur la valeur suivante " + columnName
279: + ex.getMessage());
280: }
281: return res;
282: }
283:
284: /**
285: * Execution d'une requete SQL type SELECT retournant un resultat
286: *
287: * @param sql
288: * @return
289: */
290: public DataSet executeQuery(String sql) {
291: results = null;
292: try {
293: Statement stmt = cnt.createStatement();
294: results = new DataSet(stmt.executeQuery(sql));
295: } catch (SQLException ex) {
296: org.objectweb.salome_tmf.api.Api.log(ex.getMessage());
297: }
298: return results;
299: }
300:
301: /**
302: * Execution d'une requete SQL type INSERT, UPDATE, DELETE, CREATE TABLE,
303: * DROP TABLE
304: *
305: * @param sql
306: */
307: public void executeUpdate(String sql) {
308: try {
309: Statement stmt = cnt.createStatement();
310: stmt.executeUpdate(sql);
311: } catch (SQLException ex) {
312: org.objectweb.salome_tmf.api.Api.log(ex.getMessage());
313: }
314: }
315:
316: /**
317: * Preparation d'une requete dynamique (precompilee)
318: *
319: * @param sql
320: * @return
321: */
322: public PreparedStatement prepareStatement(String sql)
323: throws SQLException {
324: PreparedStatement prep = null;
325: org.objectweb.salome_tmf.api.Api.log("prepareStatement:SQL : "
326: + sql + " with connection " + cnt);
327: prep = cnt.prepareStatement(sql);
328: return prep;
329: }
330:
331: public boolean updateDetected() {
332: try {
333: return dmd
334: .updatesAreDetected(java.sql.ResultSet.TYPE_FORWARD_ONLY);
335: } catch (Exception e) {
336: }
337: return false;
338: }
339:
340: public boolean insertDetected() {
341: try {
342: return dmd
343: .insertsAreDetected(java.sql.ResultSet.TYPE_FORWARD_ONLY);
344: } catch (Exception e) {
345: }
346: return false;
347: }
348:
349: public boolean deleteDetected() {
350: try {
351: return dmd
352: .deletesAreDetected(java.sql.ResultSet.TYPE_FORWARD_ONLY);
353: } catch (Exception e) {
354: }
355: return false;
356: }
357:
358: public void startTrans(int idTrans) {
359: String id = "" + idTrans;
360: try {
361: org.objectweb.salome_tmf.api.Api
362: .log("Transaction autocomit = "
363: + cnt.getAutoCommit());
364: java.sql.Savepoint save = cnt.setSavepoint(id);
365: savepointMap.put(id, save);
366: org.objectweb.salome_tmf.api.Api.log("Start Trans id :"
367: + id + "Save point " + save);
368: } catch (SQLException e) {
369: e.printStackTrace();
370: }
371: }
372:
373: public void rollback() throws SQLException {
374: cnt.rollback();
375:
376: }
377:
378: public void rollback(int idTrans) throws SQLException {
379: String id = "" + idTrans;
380: org.objectweb.salome_tmf.api.Api.log("ROLLBACk id :" + id
381: + "Save point " + savepointMap.get(id));
382: cnt.rollback((java.sql.Savepoint) savepointMap.get(id));
383: }
384:
385: public void commit() throws SQLException {
386: cnt.commit();
387: }
388:
389: public void setAutoCommit(boolean autoCommit) {
390: try {
391: cnt.setAutoCommit(autoCommit);
392: } catch (Exception e) {
393: }
394: }
395:
396: public boolean isClosed() {
397: try {
398: /*if (cnt==null)
399: return true;
400: else
401: return false;*/
402: org.objectweb.salome_tmf.api.Api
403: .log("Connection is close ? " + cnt.isClosed());
404: return cnt.isClosed();
405: } catch (Exception e) {
406: return true;
407: }
408: }
409:
410: }
|