001: /*
002: * This file is part of the GeOxygene project source files.
003: *
004: * GeOxygene aims at providing an open framework which implements OGC/ISO specifications for
005: * the development and deployment of geographic (GIS) applications. It is a open source
006: * contribution of the COGIT laboratory at the Institut Géographique National (the French
007: * National Mapping Agency).
008: *
009: * See: http://oxygene-project.sourceforge.net
010: *
011: * Copyright (C) 2005 Institut Géographique National
012: *
013: * This library is free software; you can redistribute it and/or modify it under the terms
014: * of the GNU Lesser General Public License as published by the Free Software Foundation;
015: * either version 2.1 of the License, or any later version.
016: *
017: * This library is distributed in the hope that it will be useful, but WITHOUT ANY
018: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
019: * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
020: *
021: * You should have received a copy of the GNU Lesser General Public License along with
022: * this library (see file LICENSE if present); if not, write to the Free Software
023: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024: *
025: */
026:
027: package fr.ign.cogit.geoxygene.datatools.castor;
028:
029: //import feature.FT_Feature;
030: //import feature.FT_FeatureCollection;
031:
032: import java.io.BufferedReader;
033: import java.io.InputStream;
034: import java.io.InputStreamReader;
035: import java.io.PrintWriter;
036: import java.sql.Connection;
037: import java.sql.ResultSet;
038: import java.sql.ResultSetMetaData;
039: import java.sql.Statement;
040: import java.util.ArrayList;
041: import java.util.List;
042:
043: import fr.ign.cogit.geoxygene.datatools.Metadata;
044:
045: //import datatools.oracle.SpatialQuery;
046:
047: /** NON MAINTENU.
048: * Implementation d'une Geodatabase utilisant Castor comme mappeur et Oracle comme SGBDR geographique.
049: * N'EST PLUS MAINTENU DEPUIS LE PASSAGE A OJB.
050: *
051: * @author Thierry Badard & Arnaud Braun
052: * @version 1.0
053: */
054:
055: public class GeodatabaseCastorOracle/* implements Geodatabase */{
056:
057: /////////////////////////////////////////////////////////////////////////////////////////
058: ///// attributs /////////////////////////////////////////////////////////////////////////
059: /////////////////////////////////////////////////////////////////////////////////////////
060: private String _oxygeneMapping; // variable d'environnememt GEOXYGENE_MAPPING
061: private Connection _conn; // connection JDBC
062: // private JDO _jdo; // JDO Castor
063: // private Database _db; // Database Castor
064: private PrintWriter _writer; // destination des messages de sortie
065: private List _metadataList; // liste des metadonnnees pour les classes persistantes.
066: private static String _databaseName = "cogit"; // nom de la base qui apparait dans le fichier database.xml
067:
068: /////////////////////////////////////////////////////////////////////////////////////////
069: ///// constructeurs /////////////////////////////////////////////////////////////////////
070: /////////////////////////////////////////////////////////////////////////////////////////
071: /** Constructeur, avec un writer pour l'affichage des messages Castor. */
072: public GeodatabaseCastorOracle(PrintWriter writer) {
073: try {
074: initWriter(writer);
075: initEnv();
076: if (writer != null)
077: writer
078: .println("Convertisseur de g?om?trie pour Oracle initialis?.");
079: initJDO(_databaseName, writer);
080: if (writer != null)
081: writer.println("Moteur JDO de Castor initialis?.");
082: initDatabase();
083: if (writer != null)
084: writer.println("Geodatabase initialis?e.");
085: initConnection();
086: if (writer != null)
087: writer.println("Connection JDBC initialis?e.");
088: initMetadata();
089: if (writer != null)
090: writer.println("M?tadonn?es initialis?es.");
091: } catch (Exception e) {
092: _writer.println(e.getMessage());
093: }
094: }
095:
096: /** Constructeur, sans writer donc sans affichage des messages Castor. */
097: public GeodatabaseCastorOracle() {
098: this (null);
099: }
100:
101: /** Constructeur sans initialiser la Geodatabase si le boolean en param?tre vaut false.
102: Utilise uniquement dans EasyLoader pour creer le fichier de mapping. */
103: public GeodatabaseCastorOracle(boolean Castor) {
104: try {
105: initWriter(null);
106: initEnv();
107: initJDO(_databaseName, null);
108: if (Castor == true) {
109: initDatabase();
110: initConnection();
111: initMetadata();
112: } else {
113: initConnectionWithoutJDO();
114: }
115: } catch (Exception e) {
116: System.out.println(e.getMessage());
117: }
118: }
119:
120: /////////////////////////////////////////////////////////////////////////////////////
121: /// initialisation des attributs ////////////////////////////////////////////////////
122: /////////////////////////////////////////////////////////////////////////////////////
123: /** Initialise le Writer. Si on avait passe null en parametre du constructeur de DataSource, alors
124: initialise un writer sur la sortie standard qui est utilise uniquement pour afficher les messages de cette classe. */
125: private void initWriter(PrintWriter writer) {
126: if (writer != null) {
127: _writer = writer;
128: } else {
129: _writer = new PrintWriter(System.out, true);
130: }
131: }
132:
133: /** Recupere la valeur de la variable d'environnement GEOXYGENE_MAPPING */
134: // on essaie d'assurer l'independance vis a vis du systeme d'exploitation
135: private void initEnv() {
136: try { // ceci est pour Windows
137: String[] command = { "cmd.exe", "/c", "set" };
138: Process pr = Runtime.getRuntime().exec(command);
139: BufferedReader br = new BufferedReader(
140: new InputStreamReader((InputStream) pr
141: .getInputStream()));
142: String ligne = br.readLine();
143: while (ligne != null) {
144: if ((ligne.length() >= 15)
145: && (ligne.substring(0, 15).compareTo(
146: "GEOXYGENE_MAPPING") == 0)) {
147: _oxygeneMapping = ligne.substring(16);
148: break;
149: }
150: ligne = br.readLine();
151: }
152: } catch (Exception e1) {
153: try { // ceci est pour Solaris / Linux
154: String command = "env";
155: Process pr = Runtime.getRuntime().exec(command);
156: BufferedReader br = new BufferedReader(
157: new InputStreamReader((InputStream) pr
158: .getInputStream()));
159: String ligne = br.readLine();
160: while (ligne != null) {
161: if ((ligne.length() >= 15)
162: && (ligne.substring(0, 15).compareTo(
163: "GEOXYGENE_MAPPING") == 0)) {
164: _oxygeneMapping = ligne.substring(16);
165: break;
166: }
167: ligne = br.readLine();
168: }
169: } catch (Exception e2) {
170: try { // ceci est pour Windows
171: String[] command = { "command.com", "/c", "set" };
172: Process pr = Runtime.getRuntime().exec(command);
173: BufferedReader br = new BufferedReader(
174: new InputStreamReader((InputStream) pr
175: .getInputStream()));
176: String ligne = br.readLine();
177: while (ligne != null) {
178: if ((ligne.length() >= 15)
179: && (ligne.substring(0, 15).compareTo(
180: "GEOXYGENE_MAPPING") == 0)) {
181: _oxygeneMapping = ligne.substring(16);
182: break;
183: }
184: ligne = br.readLine();
185: }
186: } catch (Exception e3) {
187: System.out
188: .println("GeOxygene : env, cmd ou command ne sont pas reconnus");
189: e1.printStackTrace();
190: e2.printStackTrace();
191: e3.printStackTrace();
192: }
193: }
194: }
195: if (_oxygeneMapping == null)
196: _writer
197: .println(" ### La variable d'environnement GEOXYGENE_MAPPING n'est pas renseignée ###");
198: }
199:
200: /** Initialise le JDO (moteur utilis? par Castor pour se connecter ? la base). */
201: private void initJDO(String databaseName, PrintWriter writer) {
202: /* try {
203: File databasePath = new File(_oxygeneMapping);
204: File databaseFile = new File(databasePath,"database.xml");
205: URL databaseURL = databaseFile.toURL();
206: _jdo = new JDO();
207: if (writer != null) {_jdo.setLogWriter( writer );}
208: _jdo.setConfiguration( databaseURL.toString());
209: _jdo.setDatabaseName( databaseName );
210: } catch ( Exception except ) {
211: except.printStackTrace( _writer );
212: }
213:
214: */}
215:
216: /** Ouvre une Database Castor (repr?sente une connection ouverte au SGBD) */
217: private void initDatabase() {
218: /* try {
219: _db = _jdo.getDatabase();
220: } catch ( Exception except ) {
221: except.printStackTrace( _writer );
222: }
223: */}
224:
225: /** Initialise la connection JDBC. */
226: // On cree une connection JDBC avec les parametres Castor
227: private void initConnection() {
228: /* try {
229: _conn = DatabaseRegistry.getDatabaseRegistry(_jdo.getDatabaseName()).createConnection();
230: } catch(Exception e) {
231: e.printStackTrace();
232: }
233: */}
234:
235: /** Initialise la connection JDBC sans passer par JDO. */
236: // On parse la fichier "database.xml" a l'aide des fonctionnalites XML de Castor.
237: private void initConnectionWithoutJDO() {
238: /* try {
239: File databasePath = new File(_oxygeneMapping);
240: File databaseFile = new File(databasePath,"database.xml");
241: FileReader reader = new FileReader(databaseFile);
242: datatools.castor.conf.Database _db = (datatools.castor.conf.Database)Unmarshaller.unmarshal(datatools.castor.conf.Database.class,reader);
243: java.lang.Class.forName(_db.getDriver().getClassName()).newInstance();
244: _conn = DriverManager.getConnection(_db.getDriver().getUrl(), _db.getDriver().getUserName(),
245: _db.getDriver().getPassword());
246:
247: } catch(Exception e) {
248: e.printStackTrace();
249: }
250: */}
251:
252: /** Renseigne l'attribut _metadataList. */
253: private void initMetadata() {
254: /* try {
255: MappingResolver mr = DatabaseRegistry.getDatabaseRegistry(_jdo.getDatabaseName()).getMappingResolver();
256: Enumeration listDesc = mr.listDescriptors();
257: _metadataList = new ArrayList();
258:
259: while (listDesc.hasMoreElements()) {
260: JDOClassDescriptor cd = (JDOClassDescriptor)listDesc.nextElement();
261: Metadata metadataElt = new Metadata();
262: metadataElt.setClassName(cd.getJavaClass().getName());
263: metadataElt.setTableName(cd.getTableName());
264: if (cd.getIdentity() != null) {
265: JDOFieldDescriptor fd = (JDOFieldDescriptor)cd.getIdentity();
266: metadataElt.setIdColumnName(fd.getSQLName()[0]);
267: } else _writer.println("WARNING - classe sans identifiant : "+cd.getJavaClass().getName());
268: _metadataList.add(metadataElt);
269: }
270:
271: // on recupere les parametres de la table user_sdo_geom_metadata d'Oracle
272: SpatialQuery.initGeomMetadata(_metadataList, _conn) ;
273:
274: } catch (Exception e) {
275: e.printStackTrace();
276: }
277: */}
278:
279: /////////////////////////////////////////////////////////////////////////////////////////
280: ///// gestion des transactions //////////////////////////////////////////////////////////
281: /////////////////////////////////////////////////////////////////////////////////////////
282: /** Ouvre une transaction. */
283: public void begin() {
284: /* try {
285: _db.begin();
286: } catch (Exception e) {
287: e.printStackTrace();
288: }
289: */}
290:
291: /** Commit la transaction sans la fermer. */
292: public void checkpoint() {
293: /* try {
294: _db.commit();
295: _db.begin();
296: } catch (Exception e) {
297: e.printStackTrace();
298: }
299: */}
300:
301: /** Commite et ferme la transaction. */
302: public void commit() {
303: /* try {
304: _db.commit();
305: } catch (Exception e) {
306: e.printStackTrace();
307: }
308: */}
309:
310: /** Annule et ferme la transaction. */
311: public void abort() {
312: /* try {
313: _db.rollback();
314: } catch (Exception e) {
315: e.printStackTrace();
316: }
317: */}
318:
319: /** Renvoie true si la transaction est active. */
320: /* public boolean isOpen() {
321: return _db.isActive();
322: }*/
323:
324: /** Ferme la connection (libere les ressources). */
325: public void close() {
326: /* try {
327: _db.close();
328: } catch (Exception e) {
329: e.printStackTrace();
330: }
331: */}
332:
333: /** NON IMPLEMENTE */
334: public void clearCache() {
335: System.out
336: .println("clearCache() : fonction non implementee pour Castor");
337: }
338:
339: /////////////////////////////////////////////////////////////////////////////////////////
340: ///// gestion de la persistance /////////////////////////////////////////////////////////
341: /////////////////////////////////////////////////////////////////////////////////////////
342: /** Rend persistant un objet.
343: A appeler a l'interieur d'une transaction ouverte.*/
344: public void makePersistent(Object obj) {
345: /* try {
346: _db.create(obj);
347: } catch (Exception e) {
348: e.printStackTrace();
349: }
350: */}
351:
352: /** Detruit un objet persistant.
353: A appeler a l'intwrieur d'une transaction ouverte. */
354: public void deletePersistent(Object obj) {
355: /* try {
356: _db.remove(obj);
357: } catch (Exception e) {
358: e.printStackTrace();
359: }
360: */}
361:
362: /////////////////////
363: /** A REVOIR (encapsuler dans le makePersistent). */
364: /* public void update(java.lang.Object obj) throws org.exolab.castor.jdo.ClassNotPersistenceCapableException, org.exolab.castor.jdo.TransactionNotInProgressException, org.exolab.castor.jdo.PersistenceException {
365: _db.update(obj);
366: }
367: */
368:
369: /////////////////////////////////////////////////////////////////////////////////////////
370: ///// chargement d'objets ///////////////////////////////////////////////////////////////
371: /////////////////////////////////////////////////////////////////////////////////////////
372: /** Charge l'objet d'identifiant id.
373: Passer un Integer pour id, si l'identifiant est un int.
374: Renvoie null si l'objet d'identifiant id n'existe pas.*
375: A appeler a l'interieur d'une transaction ouverte. */
376: /* public java.lang.Object load(Class clazz, Object id) {
377: try {
378: Object obj = _db.load(clazz,id);
379: return obj;
380: } catch (org.exolab.castor.jdo.ObjectNotFoundException e) {
381: System.out.println("objet non trouve - id = "+id);
382: return null;
383: } catch (Exception ee) {
384: ee.printStackTrace();
385: return null;
386: }
387: } */
388:
389: /** Charge tous les objets persistants de la classe theClass et les met dans une liste.
390: A appeler a l'interieur d'une transaction ouverte. */
391: /* public List loadAll(Class theClass) {
392: List result = new ArrayList();
393: QueryResults results;
394: OQLQuery oql;
395: try {
396: oql = _db.getOQLQuery("SELECT x FROM "+theClass.getName()+" x");
397: results = oql.execute();
398: while (results.hasMore()) { result.add(results.next());}
399: } catch (Exception e) {
400: e.printStackTrace();
401: }
402: return result;
403: } */
404:
405: /** Charge tous les FT_Feature de la classe theClass.
406: A appeler a l'interieur d'une transaction ouverte.
407: La classe theClass doit etre une sous-classe de FT_Feature, sinon renvoie une liste vide. */
408: /* public FT_FeatureCollection loadAllFeatures(Class featureClass, Class featureListClass) {
409: // FT_FeatureList result = new FT_FeatureList();
410: FT_FeatureCollection result = null;
411: try {
412: result = (FT_FeatureCollection)featureListClass.newInstance();
413: } catch (Exception e) {
414: e.printStackTrace();
415: }
416: QueryResults results;
417: OQLQuery oql;
418: if ((FT_Feature.class).isAssignableFrom(featureClass)) {
419: try {
420: String tableName = getMetadata(featureClass).getTableName();
421: oql = _db.getOQLQuery("SELECT x FROM "+featureClass.getName()+" x");
422: results = oql.execute();
423: while (results.hasMore()) { result.add((FT_Feature)results.next());}
424: } catch (Exception e) {
425: e.printStackTrace();
426: }
427: } else {
428: _writer.println("loadAllFeatures() : La classe passee en parametre n'est pas une sous-classe de FT_Feature");
429: }
430: return result;
431: }*/
432:
433: /** OBSOLETE Inactif avec Castor (renvoie un loadAllFeatures). */
434: /* public FT_FeatureList expressLoadAllFeatures(Class theClass) {
435: return loadAllFeatures(theClass);
436: }*/
437:
438: /** OBSOLETE Charge tous les FT_Feature de la classe theClass appartenant a une dalle.
439: La table doit avoir ete mise en coherence avec un dallage existant.
440: A appeler a l'interieur d'une transaction ouverte.
441: La classe theClass doit etre une sous-classe de FT_Feature, sinon renvoie une liste vide. */
442: /* public FT_FeatureList loadAllFeatures(Class theClass, int dalleNumber) {
443: FT_FeatureList result = new FT_FeatureList();
444: QueryResults results;
445: OQLQuery oql;
446: if ((FT_Feature.class).isAssignableFrom(theClass)) {
447: try {
448: oql = _db.getOQLQuery("SELECT x FROM "+theClass.getName()+" x WHERE x.dalle_id="+dalleNumber);
449: results = oql.execute();
450: while (results.hasMore()) { result.add((FT_Feature)results.next());}
451: } catch (Exception e) {
452: e.printStackTrace();
453: }
454: } else {
455: _writer.println("loadAllFeatures() : La classe passee en parametre n'est pas une sous-classe de FT_Feature");
456: }
457: return result;
458: } */
459:
460: /** Charge tous les FT_Feature de la classe theClass intersectant le GM_Object geom.
461: A appeler a l'interieur d'une transaction ouverte.
462: La classe theClass doit etre une sous-classe de FT_Feature, sinon renvoie une liste vide. */
463: /* public FT_FeatureCollection loadAllFeatures(Class featureClass, Class featureListClass, GM_Object geom) {
464: // FT_FeatureList result = new FT_FeatureList();
465: FT_FeatureCollection result = null;
466: try {
467: result = (FT_FeatureCollection)featureListClass.newInstance();
468: } catch (Exception e) {
469: e.printStackTrace();
470: }
471: if ((FT_Feature.class).isAssignableFrom(featureClass)) {
472: // on cherche la liste des identifiants
473: List idList = SpatialQuery.FT_FeatureCollection(this, featureClass, geom);
474: // charge tous les objets dont on a trouve l'identifiant
475: Iterator i = idList.iterator();
476: while (i.hasNext()) {
477: int k = ((BigDecimal)i.next()).intValue();
478: try {
479: Object obj = _db.load(featureClass, new Integer(k));
480: result.add((FT_Feature)obj);
481: } catch (Exception e) {
482: e.printStackTrace();
483: }
484: }
485: } else {
486: _writer.println("loadAllFeatures() : La classe passee en parametre n'est pas une sous-classe de FT_Feature");
487: }
488: return result;
489: } */
490:
491: /** Charge tous les FT_Feature de la classe theClass a une distance dist du GM_Object geom.
492: Si geom est la geometrie d'un FT_Feature de theClass, alors ce FT_Feature appartiendra au resultat.
493: A appeler a l'interieur d'une transaction ouverte.
494: La classe theClass doit etre une sous-classe de FT_Feature, sinon renvoie une liste vide. */
495: /* public FT_FeatureCollection loadAllFeatures(Class featureClass, Class featureListClass, GM_Object geom, double dist) {
496: // FT_FeatureList result = new FT_FeatureList();
497: FT_FeatureCollection result = null;
498: try {
499: result = (FT_FeatureCollection)featureListClass.newInstance();
500: } catch (Exception e) {
501: e.printStackTrace();
502: }
503: if ((FT_Feature.class).isAssignableFrom(featureClass)) {
504: // on cherche la liste des identifiants
505: List idList = SpatialQuery.loadAllFeatures(this, featureClass, geom, dist);
506: // charge tous les objets dont on a trouve l'identifiant
507: Iterator i = idList.iterator();
508: while (i.hasNext()) {
509: int k = ((BigDecimal)i.next()).intValue();
510: try {
511: Object obj = _db.load(featureClass, new Integer(k));
512: result.add((FT_Feature)obj);
513: } catch (Exception e) {
514: e.printStackTrace();
515: }
516: }
517: } else {
518: _writer.println("loadAllFeatures() : La classe passee en parametre n'est pas une sous-classe de FT_Feature");
519: }
520: return result;
521: } */
522:
523: /** Execute la requete OQL query, la lie avec le parametre param, et met le resultat dans une liste.
524: A appeler a l'interieur d'une transaction ouverte.
525: On peut passer null pour param, si on ne souhaite lier la requete a aucune variable. */
526: /* public List loadOQL(String query, Object param) {
527: List result = new ArrayList();
528: QueryResults results;
529: OQLQuery oql;
530: try {
531: oql = _db.getOQLQuery(query);
532: if (param != null) oql.bind(param);
533: results = oql.execute();
534: while (results.hasMore()) { result.add(results.next());}
535: } catch (Exception e) {
536: e.printStackTrace();
537: }
538: return result;
539: } */
540:
541: /////////////////////////////////////////////////////////////////////////////////////////
542: ///// OQL ///////////////////////////////////////////////////////////////////////////////
543: /////////////////////////////////////////////////////////////////////////////////////////
544: /** Cree une query vide. */
545: /* public org.exolab.castor.jdo.Query getQuery() {
546: return _db.getQuery();
547: }
548:
549: /** Cree une OQLQuery vide. */
550: /* public org.exolab.castor.jdo.OQLQuery getOQLQuery() {
551: return _db.getOQLQuery();
552: }
553:
554: /** Cree une OQLQuery avec la chaine passee en parametre. */
555: /* public org.exolab.castor.jdo.OQLQuery getOQLQuery(java.lang.String str) throws org.exolab.castor.jdo.QueryException {
556: return _db.getOQLQuery(str);
557: }
558:
559: // pour l'implementation de Geodatabase, n'a rien a voir avec Castor, renvoie null
560: /* public org.odmg.OQLQuery newOQLQuery() {
561: return null;
562: }
563: */
564:
565: /////////////////////////////////////////////////////////////////////////////////////////
566: ///// Gestion de l'information spatiale /////////////////////////////////////////////////
567: /////////////////////////////////////////////////////////////////////////////////////////
568: /** Renvoie le tableau des metadonnees. */
569: public List getMetadata() {
570: return _metadataList;
571: }
572:
573: /** Renvoie les metadonnees de la classe theClass.
574: theClass doit etre une classe definie dans le mapping.*/
575: public Metadata getMetadata(Class theClass) {
576: for (int i = 0; i < _metadataList.size(); i++)
577: if (theClass.getName().compareTo(
578: ((Metadata) _metadataList.get(i)).getClassName()) == 0)
579: return (Metadata) _metadataList.get(i);
580: System.out
581: .println("La classe n'est pas dans le fichier de mapping : "
582: + theClass.getName());
583: return null;
584: }
585:
586: /** Renvoie les metadonnees de la classe mappee avec la table theTable.
587: theTable doit etre une table d?finie dans le mapping.
588: Si theTable est mappee avec plusieurs classes, en renvoie une. */
589: public Metadata getMetadata(String theTable) {
590: for (int i = 0; i < _metadataList.size(); i++)
591: if (theTable.compareToIgnoreCase(((Metadata) _metadataList
592: .get(i)).getTableName()) == 0)
593: return (Metadata) _metadataList.get(i);
594: System.out
595: .println("La table n'est pas dans le fichier de mapping : "
596: + theTable);
597: return null;
598: }
599:
600: /** Calcule l'emprise la table mappee avec la classe.
601: La classe doit heriter de FT_Feature, la table doit contenir une geometrie. */
602: public void mbr(Class clazz) {
603: // SpatialQuery.mbr(this, clazz);
604: }
605:
606: /** Calcule un index spatial sur la table mappee avec la classe (R-Tree).
607: La classe doit heriter de FT_Feature, la table doit contenir une geometrie. */
608: public void spatialIndex(Class clazz) {
609: // SpatialQuery.spatialIndex(this, clazz);
610: }
611:
612: /////////////////////////////////////////////////////////////////////////////////////////
613: ///// SQL ///////////////////////////////////////////////////////////////////////////////
614: /////////////////////////////////////////////////////////////////////////////////////////
615: /** Renvoie la connection JDBC sous-jacente. */
616: public Connection getConnection() {
617: return _conn;
618: }
619:
620: /** Execute une commande SQL.
621: Cette commande ne doit pas renvoyer de r?sultat : INSERT, UPDATE, DELETE, mais pas SELECT. */
622: public void exeSQL(String query) {
623: try {
624: Connection conn = getConnection();
625: Statement stm = conn.createStatement();
626: stm.executeQuery(query);
627: stm.close();
628: } catch (Exception e) {
629: e.printStackTrace();
630: }
631: }
632:
633: /** Execute une requ?te et met les resultats dans une liste de tableau d'objets.
634: Les tableaux ont la taille du nombre d'objets demand?s dans le SELECT.
635: Exemple d'utilisation du resultat :
636: <tt> List edges = db.exeSQLQuery("SELECT edgeID FROM tableName WHERE ..."). </tt>
637: Pour recuperer le premier resultat :
638: <tt> edgeId = ( (BigDecimal) ((Object[]) (edges.get(0)) )[0] ).intValue(); </tt> */
639: public List exeSQLQuery(String query) {
640: List result = new ArrayList();
641: try {
642: Connection conn = getConnection();
643: Statement stm = conn.createStatement();
644: ResultSet rs = (ResultSet) stm.executeQuery(query);
645: ResultSetMetaData rsmd = rs.getMetaData();
646: int nbCol = rsmd.getColumnCount();
647: while (rs.next()) {
648: Object[] array = new Object[nbCol];
649: for (int i = 1; i <= nbCol; i++)
650: array[i - 1] = rs.getObject(i);
651: result.add(array);
652: }
653: stm.close();
654: } catch (Exception e) {
655: e.printStackTrace();
656: }
657: return result;
658: }
659:
660: /////////////////////////////////////////////////////////////////////////////////////////
661: ///// divers ////////////////////////////////////////////////////////////////////////////
662: /////////////////////////////////////////////////////////////////////////////////////////
663: /** Renvoie le nombre d'objets persistants de la classe theClass.
664: A appeler a l'interieur d'une transaction ouverte. */
665: /* public int countObjects(Class theClass) {
666: BigDecimal nn = null;
667: OQLQuery tOql;
668: QueryResults results;
669: try {
670: tOql = _db.getOQLQuery( "SELECT COUNT(*) FROM " + theClass.getName());
671: results = tOql.execute();
672: while (results.hasMore()) {nn = (BigDecimal)results.next();}
673: } catch (Exception e) {
674: e.printStackTrace();
675: }
676: return nn.intValue();
677: }
678:
679: /** Renvoie l'identifiant maximum de la classe theClass.
680: ATTENTION : La classe passee en parametre doit avoir un champ "id" de type int (marche pour les FT_Feature).
681: A appeler a l'interieur d'une transaction ouverte. */
682: /* public int maxId(Class theClass) {
683: BigDecimal nn = null;
684: OQLQuery tOql;
685: QueryResults results;
686: try {
687: tOql = _db.getOQLQuery( "SELECT MAX(x.id) FROM " + theClass.getName()+" x ");
688: results = tOql.execute();
689: while (results.hasMore()) {nn = (BigDecimal)results.next();}
690: } catch (Exception e) {
691: e.printStackTrace();
692: }
693: return nn.intValue();
694: }
695:
696: /** Renvoie l'identifiant minimum de la classe theClass.
697: ATTENTION : La classe pass?e en param?tre doit avoir un champ "id" de type int (marche pour les FT_Feature).
698: A appeler ? l'int?rieur d'une transaction ouverte. */
699: /* public int minId(Class theClass) {
700: BigDecimal nn = null;
701: OQLQuery tOql;
702: QueryResults results;
703: try {
704: tOql = _db.getOQLQuery( "SELECT MIN(x.id) FROM " + theClass.getName()+" x ");
705: results = tOql.execute();
706: while (results.hasMore()) {nn = (BigDecimal)results.next();}
707: } catch (Exception e) {
708: e.printStackTrace();
709: }
710: return nn.intValue();
711: }
712:
713:
714:
715:
716: public List loadAllElements(Class theClass, GM_Object geom) {
717: return null;
718: }
719:
720: */
721:
722: }
|