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.HashSet;
25 
26 import com.sun.star.container.XNameAccess;
27 import com.sun.star.lang.DisposedException;
28 import com.sun.star.lang.EventObject;
29 import com.sun.star.lang.XComponent;
30 import com.sun.star.lang.XEventListener;
31 import com.sun.star.lib.uno.helper.ComponentBase;
32 import com.sun.star.sdbc.SQLException;
33 import com.sun.star.sdbc.XConnection;
34 import com.sun.star.sdbc.XDatabaseMetaData;
35 import com.sun.star.sdbc.XPreparedStatement;
36 import com.sun.star.sdbc.XStatement;
37 import com.sun.star.sdbc.XWarningsSupplier;
38 import com.sun.star.uno.UnoRuntime;
39 
40 public class PostgresqlConnection extends ComponentBase implements XConnection, XWarningsSupplier, XEventListener {
41     private XConnection impl;
42     private XComponent implComponent;
43     private XWarningsSupplier implWarningsSupplier;
44     private String url;
45     private HashSet<XComponent> statements = new HashSet<>();
46 
PostgresqlConnection(XConnection impl, String url)47     public PostgresqlConnection(XConnection impl, String url) {
48         this.impl = impl;
49         implComponent = UnoRuntime.queryInterface(XComponent.class, impl);
50         implWarningsSupplier = UnoRuntime.queryInterface(XWarningsSupplier.class, impl);
51         this.url = url;
52     }
53 
54     // XComponent:
55 
56     @Override
postDisposing()57     protected synchronized void postDisposing() {
58         implComponent.dispose();
59         for (XComponent pgStatement : statements) {
60             try {
61                 pgStatement.dispose();
62             } catch (DisposedException disposedException) {
63             }
64         }
65     };
66 
67     // XEventListener:
68 
disposing(EventObject source)69     public synchronized void disposing(EventObject source) {
70         statements.remove(source.Source);
71     }
72 
73     // XWarningsSupplier:
74 
clearWarnings()75     public synchronized void clearWarnings() throws SQLException {
76         checkDisposed();
77         implWarningsSupplier.clearWarnings();
78     }
79 
getWarnings()80     public synchronized Object getWarnings() throws SQLException {
81         checkDisposed();
82         return implWarningsSupplier.getWarnings();
83     }
84 
85     // XConnection:
86 
close()87     public void close() throws SQLException {
88         dispose();
89     }
90 
commit()91     public synchronized void commit() throws SQLException {
92         checkDisposed();
93         impl.commit();
94     }
95 
createStatement()96     public synchronized XStatement createStatement() throws SQLException {
97         checkDisposed();
98         PostgresqlStatement pgStatement = new PostgresqlStatement(impl.createStatement(), this);
99         statements.add(pgStatement);
100         pgStatement.addEventListener(this);
101         return pgStatement;
102     }
103 
getAutoCommit()104     public synchronized boolean getAutoCommit() throws SQLException {
105         checkDisposed();
106         return impl.getAutoCommit();
107     }
108 
getCatalog()109     public synchronized String getCatalog() throws SQLException {
110         checkDisposed();
111         return impl.getCatalog();
112     }
113 
getMetaData()114     public synchronized XDatabaseMetaData getMetaData() throws SQLException {
115         checkDisposed();
116         return new PostgresqlDatabaseMetadata(impl.getMetaData(), this, url);
117     }
118 
getTransactionIsolation()119     public synchronized int getTransactionIsolation() throws SQLException {
120         checkDisposed();
121         return impl.getTransactionIsolation();
122     }
123 
getTypeMap()124     public synchronized XNameAccess getTypeMap() throws SQLException {
125         checkDisposed();
126         return impl.getTypeMap();
127     }
128 
isClosed()129     public synchronized boolean isClosed() throws SQLException {
130         checkDisposed();
131         return impl.isClosed();
132     }
133 
isReadOnly()134     public synchronized boolean isReadOnly() throws SQLException {
135         checkDisposed();
136         return impl.isReadOnly();
137     }
138 
nativeSQL(String arg0)139     public synchronized String nativeSQL(String arg0) throws SQLException {
140         checkDisposed();
141         return impl.nativeSQL(arg0);
142     }
143 
prepareCall(String arg0)144     public synchronized XPreparedStatement prepareCall(String arg0) throws SQLException {
145         checkDisposed();
146         PostgresqlPreparedStatement pgStatement = new PostgresqlPreparedStatement(impl.prepareCall(arg0), this);
147         statements.add(pgStatement);
148         pgStatement.addEventListener(this);
149         return pgStatement;
150     }
151 
prepareStatement(String arg0)152     public synchronized XPreparedStatement prepareStatement(String arg0) throws SQLException {
153         checkDisposed();
154         PostgresqlPreparedStatement pgStatement = new PostgresqlPreparedStatement(impl.prepareStatement(arg0), this);
155         statements.add(pgStatement);
156         pgStatement.addEventListener(this);
157         return pgStatement;
158     }
159 
rollback()160     public synchronized void rollback() throws SQLException {
161         checkDisposed();
162         impl.rollback();
163     }
164 
setAutoCommit(boolean arg0)165     public synchronized void setAutoCommit(boolean arg0) throws SQLException {
166         checkDisposed();
167         impl.setAutoCommit(arg0);
168     }
169 
setCatalog(String arg0)170     public synchronized void setCatalog(String arg0) throws SQLException {
171         checkDisposed();
172         impl.setCatalog(arg0);
173     }
174 
setReadOnly(boolean arg0)175     public synchronized void setReadOnly(boolean arg0) throws SQLException {
176         checkDisposed();
177         impl.setReadOnly(arg0);
178     }
179 
setTransactionIsolation(int arg0)180     public synchronized void setTransactionIsolation(int arg0) throws SQLException {
181         checkDisposed();
182         impl.setTransactionIsolation(arg0);
183     }
184 
setTypeMap(XNameAccess arg0)185     public synchronized void setTypeMap(XNameAccess arg0) throws SQLException {
186         checkDisposed();
187         impl.setTypeMap(arg0);
188     }
189 }
190