KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > mapper > rdb > genclass > RdbGenClassPNGIterator


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

23
24 package org.objectweb.jorm.mapper.rdb.genclass;
25
26 import org.objectweb.jorm.genclass.api.FieldDesc;
27 import org.objectweb.jorm.mapper.rdb.adapter.api.RdbAdapter;
28 import org.objectweb.jorm.api.PNameIterator;
29 import org.objectweb.jorm.api.PException;
30 import org.objectweb.jorm.api.PExceptionIO;
31
32 import java.sql.ResultSet JavaDoc;
33 import java.sql.SQLException JavaDoc;
34 import java.sql.PreparedStatement JavaDoc;
35 import java.util.NoSuchElementException JavaDoc;
36
37 /**
38  * It is an iterator over a resultset. The next() method returns the PName.
39  * @author S. Chassande-Barrioz
40  */

41 public class RdbGenClassPNGIterator
42         extends RdbGenClassNameGetter
43         implements PNameIterator {
44
45     private boolean hasNext;
46     private boolean isForw;
47     private RdbGenClassMapping gcm;
48     /**
49      * indicates if the prefetching is activated.
50      */

51     private boolean prefetching;
52
53     public RdbGenClassPNGIterator(PreparedStatement JavaDoc ps,
54                                   ResultSet JavaDoc rs,
55                                   FieldDesc[] rfds,
56                                   RdbGenClassMapping gcm,
57                                   boolean prefetching,
58                                   RdbAdapter adapter) throws SQLException JavaDoc {
59         super(ps, rs, rfds, 0, adapter);
60         this.prefetching = prefetching;
61         this.gcm = gcm;
62         hasNext = resultSet.next();
63         isForw = true;
64     }
65
66     public boolean hasNext() {
67         if (!isForw) {
68             try {
69                 hasNext = resultSet.next();
70             } catch (SQLException JavaDoc e) {
71                 e.printStackTrace();
72                 hasNext = false;
73             }
74             isForw = true;
75         }
76         return hasNext;
77     }
78
79     public Object JavaDoc next() throws NoSuchElementException JavaDoc {
80         try {
81             if (!isForw) {
82                 hasNext = resultSet.next();
83                 isForw = true;
84             }
85             if (!hasNext) {
86                 throw new NoSuchElementException JavaDoc();
87             }
88             isForw = false;
89             return (elemFields.length == 1
90                     ? gcm.decodeSingle(elemFields[0], resultSet, 1)
91                     : gcm.getPBinder().decodeAbstract(this, null));
92         } catch (Exception JavaDoc e) {
93             throw new NoSuchElementException JavaDoc(
94                     "Impossible to decode to a PName: " + e.getMessage());
95         }
96     }
97
98     public void remove() throws UnsupportedOperationException JavaDoc {
99         throw new UnsupportedOperationException JavaDoc();
100     }
101
102     public void close() throws PException {
103         if (!prefetching) {
104             hasNext = false;
105             isForw = true;
106             try {
107                 resultSet.close();
108             } catch (SQLException JavaDoc e) {
109                 throw new PExceptionIO(e);
110             }
111         }
112     }
113 }
114
Popular Tags