1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 package com.sun.star.comp.sdbc;
22 
23 import org.apache.openoffice.comp.sdbc.dbtools.util.DBTypeConversion;
24 import org.apache.openoffice.comp.sdbc.dbtools.util.Resources;
25 import org.apache.openoffice.comp.sdbc.dbtools.util.SharedResources;
26 import org.apache.openoffice.comp.sdbc.dbtools.util.StandardSQLState;
27 
28 import com.sun.star.container.XNameAccess;
29 import com.sun.star.io.XInputStream;
30 import com.sun.star.sdbc.SQLException;
31 import com.sun.star.sdbc.XArray;
32 import com.sun.star.sdbc.XBlob;
33 import com.sun.star.sdbc.XClob;
34 import com.sun.star.sdbc.XOutParameters;
35 import com.sun.star.sdbc.XRef;
36 import com.sun.star.sdbc.XRow;
37 import com.sun.star.uno.Any;
38 import com.sun.star.util.Date;
39 import com.sun.star.util.DateTime;
40 import com.sun.star.util.Time;
41 
42 public class JavaSQLCallableStatement extends JavaSQLPreparedStatement implements XRow, XOutParameters {
JavaSQLCallableStatement(JavaSQLConnection connection, String sql)43     public JavaSQLCallableStatement(JavaSQLConnection connection, String sql) {
44         super(connection, sql);
45     }
46 
47     @Override
createStatement()48     protected void createStatement() throws SQLException {
49         checkDisposed();
50         if (jdbcStatement == null) {
51             try {
52                 try {
53                     jdbcStatement = connection.getJDBCConnection().prepareCall(
54                             sqlStatement, resultSetType, resultSetConcurrency);
55                 } catch (NoSuchMethodError noSuchMethodError) {
56                     jdbcStatement = connection.getJDBCConnection().prepareCall(sqlStatement);
57                 }
58             } catch (java.sql.SQLException sqlException) {
59                 throw Tools.toUnoExceptionLogged(this, logger, sqlException);
60             }
61         }
62     }
63 
64     // XRow
65 
66     @Override
getBinaryStream(int columnIndex)67     public synchronized XInputStream getBinaryStream(int columnIndex) throws SQLException {
68         createStatement();
69         XBlob blob = getBlob(columnIndex);
70         if (blob != null) {
71             return blob.getBinaryStream();
72         }
73         return null;
74     }
75 
76     @Override
getCharacterStream(int columnIndex)77     public XInputStream getCharacterStream(int columnIndex) throws SQLException {
78         createStatement();
79         XClob clob = getClob(columnIndex);
80         if (clob != null) {
81             return clob.getCharacterStream();
82         }
83         return null;
84     }
85 
86     @Override
getBoolean(int columnIndex)87     public synchronized boolean getBoolean(int columnIndex) throws SQLException {
88         createStatement();
89         try {
90             return ((java.sql.CallableStatement)jdbcStatement).getBoolean(columnIndex);
91         } catch (java.sql.SQLException exception) {
92             throw Tools.toUnoException(this, exception);
93         }
94     }
95 
96     @Override
getByte(int columnIndex)97     public synchronized byte getByte(int columnIndex) throws SQLException {
98         createStatement();
99         try {
100             return ((java.sql.CallableStatement)jdbcStatement).getByte(columnIndex);
101         } catch (java.sql.SQLException exception) {
102             throw Tools.toUnoException(this, exception);
103         }
104     }
105 
106     @Override
getBytes(int columnIndex)107     public synchronized byte[] getBytes(int columnIndex) throws SQLException {
108         createStatement();
109         try {
110             return ((java.sql.CallableStatement)jdbcStatement).getBytes(columnIndex);
111         } catch (java.sql.SQLException exception) {
112             throw Tools.toUnoException(this, exception);
113         }
114     }
115 
116     @Override
getDate(int columnIndex)117     public synchronized Date getDate(int columnIndex) throws SQLException {
118         createStatement();
119         try {
120             java.sql.Date jdbcDate = ((java.sql.CallableStatement)jdbcStatement).getDate(columnIndex);
121             if (jdbcDate != null) {
122                 return DBTypeConversion.toDate(jdbcDate.toString());
123             } else {
124                 return new Date();
125             }
126         } catch (java.sql.SQLException exception) {
127             throw Tools.toUnoException(this, exception);
128         }
129     }
130 
131     @Override
getDouble(int columnIndex)132     public synchronized double getDouble(int columnIndex) throws SQLException {
133         createStatement();
134         try {
135             return ((java.sql.CallableStatement)jdbcStatement).getDouble(columnIndex);
136         } catch (java.sql.SQLException exception) {
137             throw Tools.toUnoException(this, exception);
138         }
139     }
140 
141     @Override
getFloat(int columnIndex)142     public synchronized float getFloat(int columnIndex) throws SQLException {
143         createStatement();
144         try {
145             return ((java.sql.CallableStatement)jdbcStatement).getFloat(columnIndex);
146         } catch (java.sql.SQLException exception) {
147             throw Tools.toUnoException(this, exception);
148         }
149     }
150 
151     @Override
getInt(int columnIndex)152     public synchronized int getInt(int columnIndex) throws SQLException {
153         createStatement();
154         try {
155             return ((java.sql.CallableStatement)jdbcStatement).getInt(columnIndex);
156         } catch (java.sql.SQLException exception) {
157             throw Tools.toUnoException(this, exception);
158         }
159     }
160 
161     @Override
getLong(int columnIndex)162     public synchronized long getLong(int columnIndex) throws SQLException {
163         createStatement();
164         try {
165             return ((java.sql.CallableStatement)jdbcStatement).getLong(columnIndex);
166         } catch (java.sql.SQLException exception) {
167             throw Tools.toUnoException(this, exception);
168         }
169     }
170 
171     @Override
getArray(int columnIndex)172     public synchronized XArray getArray(int columnIndex) throws SQLException {
173         createStatement();
174         try {
175             java.sql.Array array = ((java.sql.CallableStatement)jdbcStatement).getArray(columnIndex);
176             if (array != null) {
177                 return new JavaSQLArray(logger, array);
178             } else {
179                 return null;
180             }
181         } catch (java.sql.SQLException exception) {
182             throw Tools.toUnoException(this, exception);
183         }
184     }
185 
186     @Override
getClob(int columnIndex)187     public synchronized XClob getClob(int columnIndex) throws SQLException {
188         createStatement();
189         try {
190             java.sql.Clob clob = ((java.sql.CallableStatement)jdbcStatement).getClob(columnIndex);
191             if (clob != null) {
192                 return new JavaSQLClob(logger, clob);
193             } else {
194                 return null;
195             }
196         } catch (java.sql.SQLException exception) {
197             throw Tools.toUnoException(this, exception);
198         }
199     }
200 
201     @Override
getBlob(int columnIndex)202     public synchronized XBlob getBlob(int columnIndex) throws SQLException {
203         createStatement();
204         try {
205             java.sql.Blob blob = ((java.sql.CallableStatement)jdbcStatement).getBlob(columnIndex);
206             if (blob != null) {
207                 return new JavaSQLBlob(logger, blob);
208             } else {
209                 return null;
210             }
211         } catch (java.sql.SQLException exception) {
212             throw Tools.toUnoException(this, exception);
213         }
214     }
215 
216     @Override
getRef(int columnIndex)217     public synchronized XRef getRef(int columnIndex) throws SQLException {
218         createStatement();
219         try {
220             java.sql.Ref ref = ((java.sql.CallableStatement)jdbcStatement).getRef(columnIndex);
221             if (ref != null) {
222                 return new JavaSQLRef(ref);
223             } else {
224                 return null;
225             }
226         } catch (java.sql.SQLException exception) {
227             throw Tools.toUnoException(this, exception);
228         }
229     }
230 
231     @Override
getObject(int columnIndex, XNameAccess typeMap)232     public synchronized Object getObject(int columnIndex, XNameAccess typeMap) throws SQLException {
233         createStatement();
234         if (typeMap.hasElements()) {
235             throw new SQLException(
236                     SharedResources.getInstance().getResourceStringWithSubstitution(
237                             Resources.STR_UNSUPPORTED_FEATURE, "$featurename$", "Type maps"),
238                     this, StandardSQLState.SQL_FEATURE_NOT_IMPLEMENTED.name(), 0, Any.VOID);
239         }
240         try {
241             Object ret = Any.VOID;
242             Object object = ((java.sql.CallableStatement)jdbcStatement).getObject(columnIndex);
243             if (object instanceof String) {
244                 ret = (String) object;
245             } else if (object instanceof Boolean) {
246                 ret = (Boolean) object;
247             } else if (object instanceof java.sql.Date) {
248                 ret = DBTypeConversion.toDate(((java.sql.Date)object).toString());
249             } else if (object instanceof java.sql.Time) {
250                 ret = DBTypeConversion.toTime(((java.sql.Time)object).toString());
251             } else if (object instanceof java.sql.Timestamp) {
252                 ret = DBTypeConversion.toDateTime(((java.sql.Timestamp)object).toString());
253             }
254             return ret;
255         } catch (java.sql.SQLException exception) {
256             throw Tools.toUnoExceptionLogged(this, logger, exception);
257         }
258     }
259 
260     @Override
getShort(int columnIndex)261     public synchronized short getShort(int columnIndex) throws SQLException {
262         createStatement();
263         try {
264             return ((java.sql.CallableStatement)jdbcStatement).getShort(columnIndex);
265         } catch (java.sql.SQLException exception) {
266             throw Tools.toUnoException(this, exception);
267         }
268     }
269 
270     @Override
getString(int columnIndex)271     public synchronized String getString(int columnIndex) throws SQLException {
272         createStatement();
273         try {
274             String string = ((java.sql.CallableStatement)jdbcStatement).getString(columnIndex);
275             if (string != null) {
276                 return string;
277             } else {
278                 return "";
279             }
280         } catch (java.sql.SQLException exception) {
281             throw Tools.toUnoException(this, exception);
282         }
283     }
284 
285     @Override
getTime(int columnIndex)286     public synchronized Time getTime(int columnIndex) throws SQLException {
287         createStatement();
288         try {
289             java.sql.Time time = ((java.sql.CallableStatement)jdbcStatement).getTime(columnIndex);
290             if (time != null) {
291                 return DBTypeConversion.toTime(time.toString());
292             } else {
293                 return new Time();
294             }
295         } catch (java.sql.SQLException exception) {
296             throw Tools.toUnoException(this, exception);
297         }
298     }
299 
300     @Override
getTimestamp(int columnIndex)301     public synchronized DateTime getTimestamp(int columnIndex) throws SQLException {
302         createStatement();
303         try {
304             java.sql.Timestamp timestamp = ((java.sql.CallableStatement)jdbcStatement).getTimestamp(columnIndex);
305             if (timestamp != null) {
306                 return DBTypeConversion.toDateTime(timestamp.toString());
307             } else {
308                 return new DateTime();
309             }
310         } catch (java.sql.SQLException exception) {
311             throw Tools.toUnoException(this, exception);
312         }
313     }
314 
315     @Override
wasNull()316     public synchronized boolean wasNull() throws SQLException {
317         createStatement();
318         try {
319             return ((java.sql.CallableStatement)jdbcStatement).wasNull();
320         } catch (java.sql.SQLException exception) {
321             throw Tools.toUnoException(this, exception);
322         }
323     }
324 
325     // XOutParameters
326 
327     @Override
registerOutParameter(int index, int sqlType, String typeName)328     public synchronized void registerOutParameter(int index, int sqlType, String typeName) throws SQLException {
329         createStatement();
330         try {
331             ((java.sql.CallableStatement)jdbcStatement).registerOutParameter(index, sqlType, typeName);
332         } catch (java.sql.SQLException exception) {
333             throw Tools.toUnoException(this, exception);
334         }
335     }
336 
337     @Override
registerNumericOutParameter(int index, int sqlType, int scale)338     public void registerNumericOutParameter(int index, int sqlType, int scale) throws SQLException {
339         createStatement();
340         try {
341             ((java.sql.CallableStatement)jdbcStatement).registerOutParameter(index, sqlType, scale);
342         } catch (java.sql.SQLException exception) {
343             throw Tools.toUnoException(this, exception);
344         }
345     }
346 }
347