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 * @Author Jean-Louis Berliet
60 * @version $Revision: 1.1 $
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 * @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
80 value = result.get(property.toUpperCase());
81 if (value==null)
82 throw new MissingPropertyException(property, GroovyRowResult.class);
83 }
84 return(value);
85 }
86 catch (Exception e) {
87 throw new MissingPropertyException(property, GroovyRowResult.class, e);
88 }
89 }
90
91 /***
92 * Retrieve the value of the property by its index.
93 * A negative index will count backwards from the last column.
94 * @param index is the number of the column to look at
95 * @return the value of the property
96 */
97 public Object getAt(int index) {
98 try {
99
100 if (index<0)
101 index += result.size();
102 Iterator it = result.values().iterator();
103 int i = 0;
104 Object obj = null;
105 while ( (obj==null) && (it.hasNext()) ) {
106 if (i==index)
107 obj = it.next();
108 else
109 it.next();
110 i++;
111 }
112 return(obj);
113 }
114 catch (Exception e) {
115 throw new MissingPropertyException(Integer.toString(index), GroovyRowResult.class, e);
116 }
117 }
118
119 public String toString() {
120 return(result.toString() );
121 }
122
123 }