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.adapter; |
20 | |
21 | import static sf.qof.codegen.Constants.SIG_length; |
22 | import static sf.qof.codegen.Constants.SIG_registerOutParameter; |
23 | import static sf.qof.codegen.Constants.SIG_setNull; |
24 | import static sf.qof.codegen.Constants.TYPE_CallableStatement; |
25 | import static sf.qof.codegen.Constants.TYPE_PreparedStatement; |
26 | import static sf.qof.codegen.Constants.TYPE_ResultSet; |
27 | import static sf.qof.codegen.Constants.TYPE_String; |
28 | import static sf.qof.codegen.Constants.TYPE_int; |
29 | |
30 | import java.io.StringReader; |
31 | import java.util.HashSet; |
32 | import java.util.Set; |
33 | |
34 | import net.sf.cglib.core.CodeEmitter; |
35 | import net.sf.cglib.core.Local; |
36 | import net.sf.cglib.core.Signature; |
37 | |
38 | import org.objectweb.asm.Label; |
39 | import org.objectweb.asm.Type; |
40 | |
41 | import sf.qof.QueryObjectFactory; |
42 | import sf.qof.mapping.ParameterMapping; |
43 | import sf.qof.mapping.ResultMapping; |
44 | |
45 | /** |
46 | * ClobAdapter is a generator mapping adapter for SQL Clob data types. |
47 | * |
48 | * <p>It maps <code>Clob</code> columns to <code>String</code> and vice versa.</p> |
49 | * |
50 | * @see java.sql.Clob |
51 | */ |
52 | public class ClobAdapter implements GeneratorMappingAdapter { |
53 | |
54 | private static final Type TYPE_StringReader = Type.getType(StringReader.class); |
55 | private static final Type TYPE_Blob = Type.getType(java.sql.Blob.class); |
56 | private static final Type TYPE_ClobReader = Type.getType(ClobReader.class); |
57 | private static final Signature SIG_setCharacterStream = new Signature("setCharacterStream", "(ILjava/io/Reader;I)V"); |
58 | private static final Signature SIG_ConstructorStringReader = new Signature("<init>", "(Ljava/lang/String;)V"); |
59 | private static final Signature SIG_getClob = new Signature("getClob", "(I)Ljava/sql/Clob;"); |
60 | private static final Signature SIG_getClobNamed = new Signature("getClob", "(Ljava/lang/String;)Ljava/sql/Clob;"); |
61 | private static final Signature SIG_readClob = new Signature("readClob", "(Ljava/sql/Clob;)Ljava/lang/String;"); |
62 | |
63 | public void generateFromResult(ResultMapping resultMapping, CodeEmitter co, Local result, int[] indexes) { |
64 | co.load_local(result); |
65 | co.push(indexes[0]); |
66 | co.invoke_interface(result.getType(), SIG_getClob); |
67 | emitProcessResult(resultMapping, co); |
68 | } |
69 | |
70 | public void generateFromResultSet(ResultMapping resultMapping, CodeEmitter co, Local resultSet, String[] columns) { |
71 | co.load_local(resultSet); |
72 | co.push(columns[0]); |
73 | co.invoke_interface(TYPE_ResultSet, SIG_getClobNamed); |
74 | emitProcessResult(resultMapping, co); |
75 | } |
76 | |
77 | private void emitProcessResult(ResultMapping resultMapping, CodeEmitter co) { |
78 | Local localBlob = co.make_local(TYPE_Blob); |
79 | co.store_local(localBlob); |
80 | Label labelNull = co.make_label(); |
81 | co.load_local(localBlob); |
82 | co.ifnull(labelNull); |
83 | // not null |
84 | co.load_local(localBlob); |
85 | co.invoke_static(TYPE_ClobReader, SIG_readClob); |
86 | |
87 | Label labelEnd = co.make_label(); |
88 | co.goTo(labelEnd); |
89 | |
90 | // null |
91 | co.mark(labelNull); |
92 | co.aconst_null(); |
93 | co.mark(labelEnd); |
94 | } |
95 | |
96 | public void generateToPreparedStatement(ParameterMapping parameterMapping, CodeEmitter co, Local preparedStatement, int[] indexes, Local indexOffset) { |
97 | Local localString = co.make_local(TYPE_String); |
98 | Label labelNull = co.make_label(); |
99 | Label labelEnd = co.make_label(); |
100 | co.store_local(localString); |
101 | co.load_local(localString); |
102 | co.ifnull(labelNull); |
103 | // not null |
104 | co.load_local(preparedStatement); |
105 | co.push(indexes[0]); |
106 | if (indexOffset != null) { |
107 | co.load_local(indexOffset); |
108 | co.math(CodeEmitter.ADD, TYPE_int); |
109 | } |
110 | co.new_instance(TYPE_StringReader); |
111 | co.dup(); |
112 | co.load_local(localString); |
113 | co.invoke_constructor(TYPE_StringReader, SIG_ConstructorStringReader); |
114 | co.load_local(localString); |
115 | co.invoke_virtual(TYPE_String, SIG_length); |
116 | co.invoke_interface(TYPE_PreparedStatement, SIG_setCharacterStream); |
117 | co.goTo(labelEnd); |
118 | |
119 | co.mark(labelNull); |
120 | // null |
121 | co.load_local(preparedStatement); |
122 | co.push(indexes[0]); |
123 | if (indexOffset != null) { |
124 | co.load_local(indexOffset); |
125 | co.math(CodeEmitter.ADD, TYPE_int); |
126 | } |
127 | co.push(java.sql.Types.CLOB); |
128 | co.invoke_interface(TYPE_PreparedStatement, SIG_setNull); |
129 | |
130 | co.mark(labelEnd); |
131 | } |
132 | |
133 | public void generateRegisterOutputParameters(ResultMapping resultMapping, CodeEmitter co, Local callableStatement, |
134 | int[] indexes) { |
135 | co.load_local(callableStatement); |
136 | co.push(indexes[0]); |
137 | co.push(java.sql.Types.CLOB); |
138 | co.invoke_interface(TYPE_CallableStatement, SIG_registerOutParameter); |
139 | } |
140 | |
141 | public int getNumberOfColumns() { |
142 | return 1; |
143 | } |
144 | |
145 | public Set<Class<?>> getTypes() { |
146 | return typeSet; |
147 | } |
148 | |
149 | private static Set<Class<?>> typeSet; |
150 | |
151 | static { |
152 | typeSet = new HashSet<Class<?>>(); |
153 | typeSet.add(String.class); |
154 | } |
155 | |
156 | private final static ClobAdapter generator = new ClobAdapter(); |
157 | |
158 | /** |
159 | * Register the mapping adapter with the default name "clob". |
160 | */ |
161 | public static void register() { |
162 | register("clob"); |
163 | } |
164 | |
165 | /** |
166 | * Register the mapping adapter with the specified name. |
167 | * |
168 | * @param typeName type name |
169 | */ |
170 | public static void register(String typeName) { |
171 | QueryObjectFactory.registerMapper(typeName, generator); |
172 | } |
173 | |
174 | } |