1 | /* |
2 | * Copyright 2007 brunella ltd |
3 | * |
4 | * Licensed under the LGPL Version 3 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * |
7 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
8 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
9 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
10 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
11 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
12 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
13 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
14 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
15 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
16 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
17 | * THE POSSIBILITY OF SUCH DAMAGE. |
18 | */ |
19 | package sf.qof.mapping; |
20 | |
21 | import java.io.IOException; |
22 | import java.io.OutputStream; |
23 | import java.lang.reflect.Constructor; |
24 | import java.util.List; |
25 | |
26 | public class Mapper { |
27 | |
28 | private MethodInfo methodInfo; |
29 | private QueryType queryType; |
30 | private String sql; |
31 | private List<ParameterMapping> parameters; |
32 | private List<ResultMapping> results; |
33 | |
34 | public Mapper(MethodInfo methodInfo, QueryType type, String sql, List<ParameterMapping> parameters, |
35 | List<ResultMapping> results) { |
36 | this.methodInfo = methodInfo; |
37 | this.queryType = type; |
38 | this.sql = sql; |
39 | this.parameters = parameters; |
40 | this.results = results; |
41 | } |
42 | |
43 | public MethodInfo getMethod() { |
44 | return methodInfo; |
45 | } |
46 | |
47 | public String getSql() { |
48 | return sql; |
49 | } |
50 | |
51 | public QueryType getQueryType() { |
52 | return queryType; |
53 | } |
54 | |
55 | public List<ParameterMapping> getParameters() { |
56 | return parameters; |
57 | } |
58 | |
59 | public List<ResultMapping> getResults() { |
60 | return results; |
61 | } |
62 | |
63 | public int getNumberOfConstructorParameters() { |
64 | int num = 0; |
65 | for (ResultMapping mapping : results) { |
66 | if (mapping.getConstructorParameter() != null) { |
67 | num++; |
68 | } |
69 | } |
70 | return num; |
71 | } |
72 | |
73 | public Constructor<?> getConstructor() { |
74 | for (ResultMapping mapping : results) { |
75 | if (mapping.getConstructor() != null) { |
76 | return mapping.getConstructor(); |
77 | } |
78 | } |
79 | return null; |
80 | } |
81 | |
82 | public void acceptParameterMappers(MappingVisitor visitor) { |
83 | for (Mapping mapping : parameters) { |
84 | mapping.accept(this, visitor); |
85 | } |
86 | } |
87 | |
88 | public void acceptResultMappers(MappingVisitor visitor) { |
89 | for (Mapping mapping : results) { |
90 | mapping.accept(this, visitor); |
91 | } |
92 | } |
93 | |
94 | private void println(OutputStream out, String s) { |
95 | try { |
96 | out.write(s.getBytes()); |
97 | out.write('\n'); |
98 | } catch (IOException e) { |
99 | } |
100 | } |
101 | |
102 | public void printMappingInfo(OutputStream out) { |
103 | println(out, queryType.toString()); |
104 | println(out, methodInfo.toString()); |
105 | println(out, sql); |
106 | if (parameters != null) { |
107 | for (ParameterMapping mapping : parameters) { |
108 | println(out, mapping.parameterMappingInfo()); |
109 | } |
110 | } |
111 | if (results != null) { |
112 | for (ResultMapping mapping : results) { |
113 | println(out, mapping.resultMappingInfo()); |
114 | } |
115 | } |
116 | println(out, ""); |
117 | } |
118 | |
119 | public int getMaxParameterSqlIndex() { |
120 | int maxIndex = 0; |
121 | if (parameters != null) { |
122 | for (ParameterMapping mapping : parameters) { |
123 | for (int index : mapping.getSqlIndexes()) { |
124 | if (index > maxIndex) { |
125 | maxIndex = index; |
126 | } |
127 | } |
128 | } |
129 | } |
130 | return maxIndex; |
131 | } |
132 | |
133 | public boolean usesArray() { |
134 | if (parameters != null) { |
135 | for (ParameterMapping mapping : parameters) { |
136 | if (mapping.usesArray()) { |
137 | return true; |
138 | } |
139 | } |
140 | } |
141 | return false; |
142 | } |
143 | } |