001: /*
002: $Id: GroovyRowResult.java 3990 2006-08-18 10:25:52Z glaforge $
003:
004: Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
005:
006: Redistribution and use of this software and associated documentation
007: ("Software"), with or without modification, are permitted provided
008: that the following conditions are met:
009:
010: 1. Redistributions of source code must retain copyright
011: statements and notices. Redistributions must also contain a
012: copy of this document.
013:
014: 2. Redistributions in binary form must reproduce the
015: above copyright notice, this list of conditions and the
016: following disclaimer in the documentation and/or other
017: materials provided with the distribution.
018:
019: 3. The name "groovy" must not be used to endorse or promote
020: products derived from this Software without prior written
021: permission of The Codehaus. For written permission,
022: please contact info@codehaus.org.
023:
024: 4. Products derived from this Software may not be called "groovy"
025: nor may "groovy" appear in their names without prior written
026: permission of The Codehaus. "groovy" is a registered
027: trademark of The Codehaus.
028:
029: 5. Due credit should be given to The Codehaus -
030: http://groovy.codehaus.org/
031:
032: THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
033: ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
034: NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
035: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
036: THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
037: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
038: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
039: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
040: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
041: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
042: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
043: OF THE POSSIBILITY OF SUCH DAMAGE.
044:
045: */
046: package groovy.sql;
047:
048: import groovy.lang.GroovyObjectSupport;
049: import groovy.lang.MissingPropertyException;
050:
051: import java.util.*;
052:
053: /**
054: * Represents an extent of objects.
055: * It's used in the oneRow method to be able to access the result
056: * of a SQL query by the name of the column, or by the column number.
057: *
058: * @version $Revision: 3990 $
059: * @Author Jean-Louis Berliet
060: */
061: public class GroovyRowResult extends GroovyObjectSupport implements Map {
062:
063: private LinkedHashMap result;
064:
065: public GroovyRowResult(LinkedHashMap result) {
066: this .result = result;
067: }
068:
069: /**
070: * Retrieve the value of the property by its name *
071: *
072: * @param property is the name of the property to look at
073: * @return the value of the property
074: */
075: public Object getProperty(String property) {
076: try {
077: Object value = result.get(property);
078: if (value != null)
079: return value;
080: // if property exists and value is null, return null
081: if (result.containsKey(property))
082: return null;
083: // with some databases/drivers, the columns names are stored uppercase.
084: String propertyUpper = property.toUpperCase();
085: value = result.get(propertyUpper);
086: if (value != null)
087: return value;
088: // if property exists and value is null, return null
089: if (result.containsKey(propertyUpper))
090: return null;
091: throw new MissingPropertyException(property,
092: GroovyRowResult.class);
093: } catch (Exception e) {
094: throw new MissingPropertyException(property,
095: GroovyRowResult.class, e);
096: }
097: }
098:
099: /**
100: * Retrieve the value of the property by its index.
101: * A negative index will count backwards from the last column.
102: *
103: * @param index is the number of the column to look at
104: * @return the value of the property
105: */
106: public Object getAt(int index) {
107: try {
108: // a negative index will count backwards from the last column.
109: if (index < 0)
110: index += result.size();
111: Iterator it = result.values().iterator();
112: int i = 0;
113: Object obj = null;
114: while ((obj == null) && (it.hasNext())) {
115: if (i == index)
116: obj = it.next();
117: else
118: it.next();
119: i++;
120: }
121: return (obj);
122: } catch (Exception e) {
123: throw new MissingPropertyException(Integer.toString(index),
124: GroovyRowResult.class, e);
125: }
126: }
127:
128: public String toString() {
129: return (result.toString());
130: }
131:
132: /*
133: * The following methods are needed for implementing the Map interface.
134: * They are just delegating the request to the internal LinkedHashMap
135: */
136:
137: public void clear() {
138: result.clear();
139: }
140:
141: public boolean containsKey(Object key) {
142: return result.containsKey(key);
143: }
144:
145: public boolean containsValue(Object value) {
146: return result.containsValue(value);
147: }
148:
149: public Set entrySet() {
150: return result.entrySet();
151: }
152:
153: public boolean equals(Object o) {
154: return result.equals(o);
155: }
156:
157: public Object get(Object property) {
158: if (property instanceof String)
159: return getProperty((String) property);
160: else
161: return null;
162: }
163:
164: public int hashCode() {
165: return result.hashCode();
166: }
167:
168: public boolean isEmpty() {
169: return result.isEmpty();
170: }
171:
172: public Set keySet() {
173: return result.keySet();
174: }
175:
176: public Object put(Object key, Object value) {
177: return result.put(key, value);
178: }
179:
180: public void putAll(Map t) {
181: result.putAll(t);
182: }
183:
184: public Object remove(Object key) {
185: return result.remove(key);
186: }
187:
188: public int size() {
189: return result.size();
190: }
191:
192: public Collection values() {
193: return result.values();
194: }
195: }
|