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 
22 package com.sun.star.sdbcx.comp.postgresql;
23 
24 import java.util.concurrent.atomic.AtomicBoolean;
25 
26 import com.sun.star.beans.PropertyVetoException;
27 import com.sun.star.beans.UnknownPropertyException;
28 import com.sun.star.beans.XPropertyChangeListener;
29 import com.sun.star.beans.XPropertySet;
30 import com.sun.star.beans.XPropertySetInfo;
31 import com.sun.star.beans.XVetoableChangeListener;
32 import com.sun.star.lang.DisposedException;
33 import com.sun.star.lang.IllegalArgumentException;
34 import com.sun.star.lang.WrappedTargetException;
35 import com.sun.star.lib.uno.helper.ComponentBase;
36 import com.sun.star.sdbc.SQLException;
37 import com.sun.star.sdbc.XCloseable;
38 import com.sun.star.sdbc.XConnection;
39 import com.sun.star.sdbc.XMultipleResults;
40 import com.sun.star.sdbc.XResultSet;
41 import com.sun.star.sdbc.XStatement;
42 import com.sun.star.sdbc.XWarningsSupplier;
43 import com.sun.star.uno.UnoRuntime;
44 import com.sun.star.util.XCancellable;
45 
46 public class PostgresqlStatement extends ComponentBase
47         implements XCloseable, XPropertySet, XCancellable, XStatement, XWarningsSupplier, XMultipleResults {
48 
49     private XStatement impl;
50     private XCloseable implCloseable;
51     private XPropertySet implPropertySet;
52     private XCancellable implCancellable;
53     private XWarningsSupplier implWarningsSupplier;
54     private XMultipleResults implMultipleResults;
55     private XConnection connection;
56     private AtomicBoolean isDisposed = new AtomicBoolean(false);
57 
58     public PostgresqlStatement(XStatement impl, XConnection connection) {
59         this.impl = impl;
60         this.implCloseable = UnoRuntime.queryInterface(XCloseable.class, impl);
61         this.implPropertySet = UnoRuntime.queryInterface(XPropertySet.class, impl);
62         this.implCancellable = UnoRuntime.queryInterface(XCancellable.class, impl);
63         this.implWarningsSupplier = UnoRuntime.queryInterface(XWarningsSupplier.class, impl);
64         this.implMultipleResults = UnoRuntime.queryInterface(XMultipleResults.class, impl);
65         this.connection = connection;
66     }
67 
68     // XComponentBase:
69 
70     @Override
71     protected void postDisposing() {
72         isDisposed.set(true);
73         try {
74             implCloseable.close();
75         } catch (SQLException sqlException) {
76         }
77     }
78 
79     private void checkDisposed() throws DisposedException {
80         if (isDisposed.get()) {
81             throw new DisposedException();
82         }
83     }
84 
85     // XStatement:
86 
87     public boolean execute(String arg0) throws SQLException {
88         checkDisposed();
89         System.out.println(arg0);
90         return impl.execute(arg0);
91     }
92 
93     public XResultSet executeQuery(String arg0) throws SQLException {
94         checkDisposed();
95         XResultSet results = impl.executeQuery(arg0);
96         return new PostgresqlResultSet(results, this);
97     }
98 
99     public int executeUpdate(String arg0) throws SQLException {
100         checkDisposed();
101         return impl.executeUpdate(arg0);
102     }
103 
104     public XConnection getConnection() throws SQLException {
105         checkDisposed();
106         return connection;
107     }
108 
109     // XCloseable:
110 
111     public void close() throws SQLException {
112         dispose();
113     }
114 
115     // XPropertySet:
116 
117     public void addPropertyChangeListener(String arg0, XPropertyChangeListener arg1) throws UnknownPropertyException, WrappedTargetException {
118         checkDisposed();
119         implPropertySet.addPropertyChangeListener(arg0, arg1);
120     }
121 
122     public void addVetoableChangeListener(String arg0, XVetoableChangeListener arg1) throws UnknownPropertyException, WrappedTargetException {
123         checkDisposed();
124         implPropertySet.addVetoableChangeListener(arg0, arg1);
125     }
126 
127     public XPropertySetInfo getPropertySetInfo() {
128         checkDisposed();
129         return implPropertySet.getPropertySetInfo();
130     }
131 
132     public Object getPropertyValue(String arg0) throws UnknownPropertyException, WrappedTargetException {
133         checkDisposed();
134         return implPropertySet.getPropertyValue(arg0);
135     }
136 
137     public void removePropertyChangeListener(String arg0, XPropertyChangeListener arg1) throws UnknownPropertyException, WrappedTargetException {
138         checkDisposed();
139         implPropertySet.removePropertyChangeListener(arg0, arg1);
140     }
141 
142     public void removeVetoableChangeListener(String arg0, XVetoableChangeListener arg1) throws UnknownPropertyException, WrappedTargetException {
143         checkDisposed();
144         implPropertySet.removeVetoableChangeListener(arg0, arg1);
145     }
146 
147     public void setPropertyValue(String arg0, Object arg1)
148             throws UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException {
149         checkDisposed();
150         implPropertySet.setPropertyValue(arg0, arg1);
151     }
152 
153     // XCancellable:
154 
155     public void cancel() {
156         checkDisposed();
157         implCancellable.cancel();
158     }
159 
160     // XWarningsSupplier:
161 
162     public void clearWarnings() throws SQLException {
163         checkDisposed();
164         implWarningsSupplier.clearWarnings();
165     }
166 
167     public Object getWarnings() throws SQLException {
168         checkDisposed();
169         return implWarningsSupplier.getWarnings();
170     }
171 
172     // XMultipleResults:
173 
174     public boolean getMoreResults() throws SQLException {
175         checkDisposed();
176         return implMultipleResults.getMoreResults();
177     }
178 
179     public XResultSet getResultSet() throws SQLException {
180         checkDisposed();
181         return new PostgresqlResultSet(implMultipleResults.getResultSet(), this);
182     }
183 
184     public int getUpdateCount() throws SQLException {
185         checkDisposed();
186         return implMultipleResults.getUpdateCount();
187     }
188 }
189