KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > queryframework > DirectReadQuery


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.queryframework;
23
24 import java.util.*;
25 import oracle.toplink.essentials.mappings.converters.*;
26 import oracle.toplink.essentials.internal.helper.*;
27 import oracle.toplink.essentials.exceptions.*;
28 import oracle.toplink.essentials.internal.queryframework.*;
29 import oracle.toplink.essentials.internal.sessions.AbstractRecord;
30
31 /**
32  * <p><b>Purpose</b>:
33  * Concrete class to perform a direct read.
34  * <p>
35  * <p><b>Responsibilities</b>:
36  * Used in conjunction with DirectCollectionMapping.
37  * This can be used to read a single column of data (i.e. one field).
38  * A container (implementing Collection) of the data values is returned.
39  *
40  * @author Yvon Lavoie
41  * @since TOPLink/Java 1.0
42  */

43 public class DirectReadQuery extends DataReadQuery {
44
45     /** Allows user defined conversion between the result value and the database value. */
46     protected Converter valueConverter;
47
48     /**
49      * PUBLIC:
50      * Initialize the state of the query.
51      */

52     public DirectReadQuery() {
53         super();
54     }
55
56     /**
57      * PUBLIC:
58      * Initialize the query to use the specified SQL string.
59      */

60     public DirectReadQuery(String JavaDoc sqlString) {
61         super(sqlString);
62     }
63
64     /**
65      * PUBLIC:
66      * Initialize the query to use the specified call.
67      */

68     public DirectReadQuery(Call call) {
69         super(call);
70     }
71
72     /**
73      * PUBLIC:
74      * Return the converter on the query.
75      * A converter can be used to convert between the result value and database value.
76      */

77     public Converter getValueConverter() {
78         return valueConverter;
79     }
80
81     /**
82      * PUBLIC:
83      * Set the converter on the query.
84      * A converter can be used to convert between the result value and database value.
85      */

86     public void setValueConverter(Converter valueConverter) {
87         this.valueConverter = valueConverter;
88     }
89
90     /**
91      * INTERNAL:
92      * Used by cursored stream.
93      * Return the first field in the row.
94      */

95     public Object JavaDoc buildObject(AbstractRecord row) {
96         Object JavaDoc value = row.get(row.getFields().firstElement());
97         if (getValueConverter() != null) {
98             value = getValueConverter().convertDataValueToObjectValue(value, session);
99         }
100         return value;
101     }
102
103     /**
104      * INTERNAL:
105      * The results are *not* in a cursor, build the collection.
106      */

107     public Object JavaDoc executeNonCursor() throws DatabaseException, QueryException {
108         ContainerPolicy cp = getContainerPolicy();
109
110         Vector rows = getQueryMechanism().executeSelect();
111         Object JavaDoc result = cp.containerInstance(rows.size());
112         DatabaseField resultDirectField = null;
113
114         for (Enumeration stream = rows.elements(); stream.hasMoreElements();) {
115             AbstractRecord row = (AbstractRecord)stream.nextElement();
116             if (resultDirectField == null) {
117                 resultDirectField = (DatabaseField)row.getFields().firstElement();
118             }
119             Object JavaDoc value = row.get(resultDirectField);
120             if (getValueConverter() != null) {
121                 value = getValueConverter().convertDataValueToObjectValue(value, session);
122             }
123             cp.addInto(value, result, getSession());
124         }
125         return result;
126     }
127 }
128
Popular Tags