Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*************************************************************************
3 : *
4 : * Effective License of whole file:
5 : *
6 : * This library is free software; you can redistribute it and/or
7 : * modify it under the terms of the GNU Lesser General Public
8 : * License version 2.1, as published by the Free Software Foundation.
9 : *
10 : * This library is distributed in the hope that it will be useful,
11 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : * Lesser General Public License for more details.
14 : *
15 : * You should have received a copy of the GNU Lesser General Public
16 : * License along with this library; if not, write to the Free Software
17 : * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18 : * MA 02111-1307 USA
19 : *
20 : * Parts "Copyright by Sun Microsystems, Inc" prior to August 2011:
21 : *
22 : * The Contents of this file are made available subject to the terms of
23 : * the GNU Lesser General Public License Version 2.1
24 : *
25 : * Copyright: 2000 by Sun Microsystems, Inc.
26 : *
27 : * Contributor(s): Joerg Budischewski
28 : *
29 : * All parts contributed on or after August 2011:
30 : *
31 : * This Source Code Form is subject to the terms of the Mozilla Public
32 : * License, v. 2.0. If a copy of the MPL was not distributed with this
33 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
34 : *
35 : ************************************************************************/
36 :
37 : #ifndef INCLUDED_CONNECTIVITY_SOURCE_DRIVERS_POSTGRESQL_PQ_CONNECTION_HXX
38 : #define INCLUDED_CONNECTIVITY_SOURCE_DRIVERS_POSTGRESQL_PQ_CONNECTION_HXX
39 :
40 : #include <config_lgpl.h>
41 : #include <boost/unordered_map.hpp>
42 : #include <com/sun/star/uno/XComponentContext.hpp>
43 : #include <com/sun/star/lang/XInitialization.hpp>
44 : #include <com/sun/star/script/XTypeConverter.hpp>
45 : #include <com/sun/star/sdbc/XWarningsSupplier.hpp>
46 : #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
47 : #include <com/sun/star/sdbcx/XUsersSupplier.hpp>
48 : #include <com/sun/star/sdbcx/XViewsSupplier.hpp>
49 : #include <com/sun/star/sdbc/XConnection.hpp>
50 :
51 : #include <com/sun/star/container/XNameAccess.hpp>
52 :
53 : #include <rtl/ref.hxx>
54 : #include <rtl/byteseq.hxx>
55 :
56 : #include <salhelper/simplereferenceobject.hxx>
57 :
58 : #include <cppuhelper/weakref.hxx>
59 : #include <cppuhelper/compbase6.hxx>
60 :
61 : #include <libpq-fe.h>
62 :
63 : namespace pq_sdbc_driver
64 : {
65 : #ifdef POSTGRE_TRACE
66 : #define POSTGRE_TRACE( x ) printf( "%s\n" , x )
67 : #else
68 : #define POSTGRE_TRACE(x) ((void)0)
69 : #endif
70 :
71 0 : class RefCountedMutex : public salhelper::SimpleReferenceObject
72 : {
73 : public:
74 : osl::Mutex mutex;
75 : };
76 :
77 : struct ConnectionSettings;
78 :
79 :
80 :
81 :
82 : // Logging API
83 :
84 : namespace LogLevel
85 : {
86 : // when you add a loglevel, extend the log function !
87 : static const sal_Int32 NONE = 0;
88 : static const sal_Int32 ERROR = 1;
89 : static const sal_Int32 SQL = 2;
90 : static const sal_Int32 INFO = 3;
91 : static const sal_Int32 DATA = 4;
92 : }
93 : bool isLog( ConnectionSettings *settings, int loglevel );
94 : void log( ConnectionSettings *settings, sal_Int32 level, const OUString &logString );
95 : void log( ConnectionSettings *settings, sal_Int32 level, const char *str );
96 :
97 :
98 : class Tables;
99 : class Views;
100 0 : struct ConnectionSettings
101 : {
102 0 : ConnectionSettings() :
103 : encoding( RTL_TEXTENCODING_UTF8),
104 : pConnection(0),
105 : maxNameLen(0),
106 : maxIndexKeys(0),
107 : pTablesImpl(0),
108 : pViewsImpl(0),
109 : showSystemColumns( false ),
110 : logFile( 0 ),
111 0 : loglevel( LogLevel::INFO )
112 0 : {}
113 : rtl_TextEncoding encoding;
114 : PGconn *pConnection;
115 : sal_Int32 maxNameLen;
116 : sal_Int32 maxIndexKeys;
117 : ::com::sun::star::uno::Reference< com::sun::star::script::XTypeConverter > tc;
118 : ::com::sun::star::uno::Reference< com::sun::star::container::XNameAccess > tables;
119 : ::com::sun::star::uno::Reference< com::sun::star::container::XNameAccess > users;
120 : ::com::sun::star::uno::Reference< com::sun::star::container::XNameAccess > views;
121 : Tables *pTablesImpl; // needed to implement renaming of tables / views
122 : Views *pViewsImpl; // needed to implement renaming of tables / views
123 : OUString user;
124 : OUString catalog;
125 : bool showSystemColumns;
126 : FILE *logFile;
127 : sal_Int32 loglevel;
128 : };
129 :
130 :
131 : typedef cppu::WeakComponentImplHelper6<
132 : com::sun::star::sdbc::XConnection,
133 : com::sun::star::sdbc::XWarningsSupplier,
134 : com::sun::star::lang::XInitialization,
135 : com::sun::star::sdbcx::XTablesSupplier,
136 : com::sun::star::sdbcx::XViewsSupplier,
137 : com::sun::star::sdbcx::XUsersSupplier > ConnectionBase;
138 :
139 : // some types
140 : struct HashByteSequence
141 : {
142 0 : sal_Int32 operator () ( const ::rtl::ByteSequence & seq ) const
143 : {
144 0 : return *(sal_Int32*) seq.getConstArray();
145 : }
146 : };
147 :
148 : typedef ::boost::unordered_map<
149 : ::rtl::ByteSequence,
150 : ::com::sun::star::uno::WeakReference< com::sun::star::sdbc::XCloseable >,
151 : HashByteSequence,
152 : ::std::equal_to< ::rtl::ByteSequence >
153 : > WeakHashMap;
154 : typedef ::std::vector< OString > OStringVector;
155 :
156 :
157 :
158 : typedef ::boost::unordered_map
159 : <
160 : const sal_Int32,
161 : OUString,
162 : ::boost::hash< sal_Int32 >,
163 : ::std::equal_to< sal_Int32 >
164 : > Int2StringMap;
165 :
166 : class Connection : public ConnectionBase
167 : {
168 : ::com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext > m_ctx;
169 : ::com::sun::star::uno::Reference< com::sun::star::container::XNameAccess > m_typeMap;
170 : ConnectionSettings m_settings;
171 : ::rtl::Reference< RefCountedMutex > m_refMutex;
172 : ::com::sun::star::uno::Reference< com::sun::star::sdbc::XDatabaseMetaData > m_meta;
173 : WeakHashMap m_myStatements;
174 :
175 : private:
176 : void checkClosed()
177 : throw ( com::sun::star::sdbc::SQLException, com::sun::star::uno::RuntimeException );
178 :
179 : public:
180 : Connection(
181 : const rtl::Reference< RefCountedMutex > &refMutex,
182 : const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > & ctx );
183 :
184 : virtual ~Connection( );
185 :
186 : public: // XCloseable
187 : virtual void SAL_CALL close()
188 : throw ( ::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
189 :
190 : public: // XConnection
191 :
192 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XStatement > SAL_CALL createStatement( )
193 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE ;
194 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XPreparedStatement > SAL_CALL prepareStatement(
195 : const OUString& sql )
196 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
197 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XPreparedStatement > SAL_CALL prepareCall(
198 : const OUString& sql )
199 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
200 : virtual OUString SAL_CALL nativeSQL( const OUString& sql )
201 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
202 : virtual void SAL_CALL setAutoCommit( sal_Bool autoCommit )
203 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
204 : virtual sal_Bool SAL_CALL getAutoCommit( )
205 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
206 : virtual void SAL_CALL commit( )
207 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
208 : virtual void SAL_CALL rollback( )
209 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
210 : virtual sal_Bool SAL_CALL isClosed( )
211 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
212 : virtual ::com::sun::star::uno::Reference< com::sun::star::sdbc::XDatabaseMetaData > SAL_CALL getMetaData( )
213 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
214 : virtual void SAL_CALL setReadOnly( sal_Bool readOnly )
215 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
216 : virtual sal_Bool SAL_CALL isReadOnly( )
217 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
218 : virtual void SAL_CALL setCatalog( const OUString& catalog )
219 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
220 : virtual OUString SAL_CALL getCatalog( )
221 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
222 : virtual void SAL_CALL setTransactionIsolation( sal_Int32 level )
223 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
224 : virtual sal_Int32 SAL_CALL getTransactionIsolation( )
225 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
226 : virtual ::com::sun::star::uno::Reference< com::sun::star::container::XNameAccess > SAL_CALL getTypeMap( )
227 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
228 : virtual void SAL_CALL setTypeMap(
229 : const ::com::sun::star::uno::Reference< com::sun::star::container::XNameAccess >& typeMap )
230 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
231 :
232 : public: // XWarningsSupplier
233 : virtual ::com::sun::star::uno::Any SAL_CALL getWarnings( )
234 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
235 : virtual void SAL_CALL clearWarnings( )
236 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
237 :
238 : public: // XInitialization
239 : virtual void SAL_CALL initialize(
240 : const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aArguments )
241 : throw (com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
242 :
243 : public: // XTablesSupplier
244 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess > SAL_CALL getTables( ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
245 :
246 : public: // XUsersSupplier
247 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess > SAL_CALL getUsers( ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
248 :
249 : public: // XViewsSupplier
250 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess > SAL_CALL getViews( ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
251 :
252 : public:
253 : virtual void SAL_CALL disposing() SAL_OVERRIDE;
254 :
255 : public: // helper function
256 : void removeFromWeakMap( const ::rtl::ByteSequence & seq );
257 : };
258 :
259 : }
260 : #endif
261 :
262 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|