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.Iterator;
52 import java.util.LinkedHashMap;
53
54 /***
55 * Represents an extent of objects.
56 * It's used in the oneRow method to be able to access the result
57 * of a SQL query by the name of the column, or by the column number.
58 *
59 * @version $Revision: 1.2 $
60 * @Author Jean-Louis Berliet
61 */
62 public class GroovyRowResult extends GroovyObjectSupport {
63
64 private LinkedHashMap result;
65
66 public GroovyRowResult(LinkedHashMap result) {
67 this.result = result;
68 }
69
70 /***
71 * Retrieve the value of the property by its name *
72 *
73 * @param property is the name of the property to look at
74 * @return the value of the property
75 */
76 public Object getProperty(String property) {
77 try {
78 Object value = result.get(property);
79 if (value == null) {
80
81 value = result.get(property.toUpperCase());
82 if (value == null)
83 throw new MissingPropertyException(property, GroovyRowResult.class);
84 }
85 return (value);
86 }
87 catch (Exception e) {
88 throw new MissingPropertyException(property, GroovyRowResult.class, e);
89 }
90 }
91
92 /***
93 * Retrieve the value of the property by its index.
94 * A negative index will count backwards from the last column.
95 *
96 * @param index is the number of the column to look at
97 * @return the value of the property
98 */
99 public Object getAt(int index) {
100 try {
101
102 if (index < 0)
103 index += result.size();
104 Iterator it = result.values().iterator();
105 int i = 0;
106 Object obj = null;
107 while ((obj == null) && (it.hasNext())) {
108 if (i == index)
109 obj = it.next();
110 else
111 it.next();
112 i++;
113 }
114 return (obj);
115 }
116 catch (Exception e) {
117 throw new MissingPropertyException(Integer.toString(index), GroovyRowResult.class, e);
118 }
119 }
120
121 public String toString() {
122 return (result.toString());
123 }
124 }