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.Resources;
24 
25 import com.sun.star.lang.XServiceInfo;
26 import com.sun.star.logging.LogLevel;
27 import com.sun.star.sdbc.SQLException;
28 import com.sun.star.sdbc.XBatchExecution;
29 import com.sun.star.sdbc.XConnection;
30 import com.sun.star.sdbc.XResultSet;
31 import com.sun.star.sdbc.XStatement;
32 
33 public class JavaSQLStatement extends JavaSQLStatementBase
34         implements XStatement, XServiceInfo, XBatchExecution {
35 
36     private static final String[] services = {
37             "com.sun.star.sdbc.Statement"
38     };
39 
JavaSQLStatement(JavaSQLConnection connection)40     public JavaSQLStatement(JavaSQLConnection connection) {
41         super(connection);
42     }
43 
44     // XServiceInfo
45 
46     @Override
getImplementationName()47     public String getImplementationName() {
48         return "com.sun.star.sdbcx.JStatement";
49     }
50 
51     @Override
getSupportedServiceNames()52     public String[] getSupportedServiceNames() {
53         return services.clone();
54     }
55 
56     @Override
supportsService(String serviceName)57     public boolean supportsService(String serviceName) {
58         for (String service : services) {
59             if (service.equals(serviceName)) {
60                 return true;
61             }
62         }
63         return false;
64     }
65 
66     // XBatchExecution
67 
68     @Override
addBatch(String sql)69     public synchronized void addBatch(String sql) throws SQLException {
70         createStatement();
71         try {
72             jdbcStatement.addBatch(sql);
73         } catch (java.sql.SQLException sqlException) {
74             throw Tools.toUnoException(this, sqlException);
75         }
76     }
77 
78     @Override
clearBatch()79     public void clearBatch() throws SQLException {
80         createStatement();
81         try {
82             jdbcStatement.clearBatch();
83         } catch (java.sql.SQLException sqlException) {
84             throw Tools.toUnoException(this, sqlException);
85         }
86     }
87 
88     @Override
executeBatch()89     public synchronized int[] executeBatch() throws SQLException {
90         createStatement();
91         try {
92             return jdbcStatement.executeBatch();
93         } catch (java.sql.SQLException sqlException) {
94             throw Tools.toUnoException(this, sqlException);
95         }
96     }
97 
98     // XStatement
99 
100     @Override
execute(String sql)101     public synchronized boolean execute(String sql) throws SQLException {
102         createStatement();
103         sqlStatement = sql;
104         try (ContextClassLoaderScope ccl = new ContextClassLoaderScope(connection.getDriverClassLoader())) {
105             return jdbcStatement.execute(sql);
106         } catch (java.sql.SQLException sqlException) {
107             throw Tools.toUnoExceptionLogged(this, logger, sqlException);
108         }
109     }
110 
111     @Override
executeQuery(String sql)112     public synchronized XResultSet executeQuery(String sql) throws SQLException {
113         logger.log(LogLevel.FINE, Resources.STR_LOG_EXECUTE_QUERY, sql);
114         createStatement();
115         sqlStatement = sql;
116         try (ContextClassLoaderScope ccl = new ContextClassLoaderScope(connection.getDriverClassLoader())) {
117             java.sql.ResultSet jdbcResultSet = jdbcStatement.executeQuery(sql);
118             if (jdbcResultSet != null) {
119                 return new JavaSQLResultSet(jdbcResultSet, connection, this);
120             } else {
121                 return null;
122             }
123         } catch (java.sql.SQLException sqlException) {
124             throw Tools.toUnoExceptionLogged(this, logger, sqlException);
125         }
126     }
127 
128     @Override
executeUpdate(String sql)129     public synchronized int executeUpdate(String sql) throws SQLException {
130         createStatement();
131         logger.log(LogLevel.FINE, Resources.STR_LOG_EXECUTE_UPDATE, sql);
132         sqlStatement = sql;
133         // FIXME: why didn't the C++ implementation do this in a ContextClassLoaderScope?
134         try {
135             return jdbcStatement.executeUpdate(sql);
136         } catch (java.sql.SQLException sqlException) {
137             throw Tools.toUnoExceptionLogged(this, logger, sqlException);
138         }
139     }
140 
141     @Override
getConnection()142     public synchronized XConnection getConnection() throws SQLException {
143         checkDisposed();
144         return connection;
145     }
146 
147     // others
148 
149     @Override
createStatement()150     protected synchronized void createStatement() throws SQLException {
151         checkDisposed();
152         if (jdbcStatement == null) {
153             try {
154                 try {
155                     jdbcStatement = connection.getJDBCConnection().createStatement(resultSetType, resultSetConcurrency);
156                 } catch (NoSuchMethodError noSuchMethodError) {
157                     jdbcStatement = connection.getJDBCConnection().createStatement();
158                 }
159             } catch (java.sql.SQLException sqlException) {
160                 throw Tools.toUnoExceptionLogged(this, logger, sqlException);
161             }
162         }
163     }
164 }
165