View Javadoc

1   /*
2   $Id: GroovyRowResult.java 3990 2006-08-18 10:25:52Z glaforge $
3   
4   Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
5   
6   Redistribution and use of this software and associated documentation
7   ("Software"), with or without modification, are permitted provided
8   that the following conditions are met:
9   
10  1. Redistributions of source code must retain copyright
11     statements and notices.  Redistributions must also contain a
12     copy of this document.
13  
14  2. Redistributions in binary form must reproduce the
15     above copyright notice, this list of conditions and the
16     following disclaimer in the documentation and/or other
17     materials provided with the distribution.
18  
19  3. The name "groovy" must not be used to endorse or promote
20     products derived from this Software without prior written
21     permission of The Codehaus.  For written permission,
22     please contact info@codehaus.org.
23  
24  4. Products derived from this Software may not be called "groovy"
25     nor may "groovy" appear in their names without prior written
26     permission of The Codehaus. "groovy" is a registered
27     trademark of The Codehaus.
28  
29  5. Due credit should be given to The Codehaus -
30     http://groovy.codehaus.org/
31  
32  THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
33  ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
34  NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
35  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
36  THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
37  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
43  OF THE POSSIBILITY OF SUCH DAMAGE.
44  
45  */
46  package groovy.sql;
47  
48  import groovy.lang.GroovyObjectSupport;
49  import groovy.lang.MissingPropertyException;
50  
51  import java.util.*;
52  
53  /***
54   * Represents an extent of objects.
55   * It's used in the oneRow method to be able to access the result
56   * of a SQL query by the name of the column, or by the column number.
57   *
58   * @version $Revision: 3990 $
59   * @Author Jean-Louis Berliet
60   */
61  public class GroovyRowResult extends GroovyObjectSupport implements Map {
62  
63      private LinkedHashMap result;
64  
65      public GroovyRowResult(LinkedHashMap result) {
66          this.result = result;
67      }
68  
69      /***
70       * Retrieve the value of the property by its name    *
71       *
72       * @param property is the name of the property to look at
73       * @return the value of the property
74       */
75      public Object getProperty(String property) {
76          try {
77              Object value = result.get(property);
78              if (value != null)
79                  return value;
80              // if property exists and value is null, return null
81              if (result.containsKey(property))
82                  return null;
83              // with some databases/drivers, the columns names are stored uppercase.
84              String propertyUpper = property.toUpperCase();
85              value = result.get(propertyUpper);
86              if (value != null)
87                  return value;
88              // if property exists and value is null, return null
89              if (result.containsKey(propertyUpper)) 
90                  return null;
91              throw new MissingPropertyException(property, GroovyRowResult.class);
92          }
93          catch (Exception e) {
94              throw new MissingPropertyException(property, GroovyRowResult.class, e);
95          }
96      }
97  
98      /***
99       * Retrieve the value of the property by its index.
100      * A negative index will count backwards from the last column.
101      *
102      * @param index is the number of the column to look at
103      * @return the value of the property
104      */
105     public Object getAt(int index) {
106         try {
107             // a negative index will count backwards from the last column.
108             if (index < 0)
109                 index += result.size();
110             Iterator it = result.values().iterator();
111             int i = 0;
112             Object obj = null;
113             while ((obj == null) && (it.hasNext())) {
114                 if (i == index)
115                     obj = it.next();
116                 else
117                     it.next();
118                 i++;
119             }
120             return (obj);
121         }
122         catch (Exception e) {
123             throw new MissingPropertyException(Integer.toString(index), GroovyRowResult.class, e);
124         }
125     }
126 
127     public String toString() {
128         return (result.toString());
129     }
130 
131     /*
132      * The following methods are needed for implementing the Map interface.
133      * They are just delegating the request to the internal LinkedHashMap
134      */
135      
136     public void clear() {
137         result.clear();
138     }
139 
140     public boolean containsKey(Object key) {
141         return result.containsKey(key);
142     }
143 
144     public boolean containsValue(Object value) {
145         return result.containsValue(value);
146     }
147 
148     public Set entrySet() {
149         return result.entrySet();
150     }
151 
152     public boolean equals(Object o) {
153         return result.equals(o);
154     }
155 
156     public Object get(Object property) {
157         if (property instanceof String)
158             return getProperty((String)property);
159         else
160             return null;
161     }
162 
163     public int hashCode() {
164         return result.hashCode();
165     }
166 
167     public boolean isEmpty() {
168         return result.isEmpty();
169     }
170 
171     public Set keySet() {
172         return result.keySet();
173     }
174 
175     public Object put(Object key, Object value) {
176         return result.put(key, value);
177     }
178 
179     public void putAll(Map t) {
180         result.putAll(t);
181     }
182 
183     public Object remove(Object key) {
184         return result.remove(key);
185     }
186 
187     public int size() {
188         return result.size();
189     }
190 
191     public Collection values() {
192         return result.values();
193     }
194 }