1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
81 if (result.containsKey(property))
82 return null;
83
84 String propertyUpper = property.toUpperCase();
85 value = result.get(propertyUpper);
86 if (value != null)
87 return value;
88
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
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
133
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 }