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 : * Version: MPL 1.1 / GPLv3+ / LGPLv2.1+
32 : *
33 : * The contents of this file are subject to the Mozilla Public License Version
34 : * 1.1 (the "License"); you may not use this file except in compliance with
35 : * the License or as specified alternatively below. You may obtain a copy of
36 : * the License at http://www.mozilla.org/MPL/
37 : *
38 : * Software distributed under the License is distributed on an "AS IS" basis,
39 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
40 : * for the specific language governing rights and limitations under the
41 : * License.
42 : *
43 : * Major Contributor(s):
44 : * [ Copyright (C) 2011 Lionel Elie Mamane <lionel@mamane.lu> ]
45 : *
46 : * All Rights Reserved.
47 : *
48 : * For minor contributions see the git repository.
49 : *
50 : * Alternatively, the contents of this file may be used under the terms of
51 : * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
52 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPLv2.1+"),
53 : * in which case the provisions of the GPLv3+ or the LGPLv2.1+ are applicable
54 : * instead of those above.
55 : *
56 : ************************************************************************/
57 :
58 : #ifndef _PQ_CONNECTION_HXX_
59 : #define _PQ_CONNECTION_HXX_
60 : #include <boost/unordered_map.hpp>
61 : #include <com/sun/star/uno/XComponentContext.hpp>
62 : #include <com/sun/star/lang/XInitialization.hpp>
63 : #include <com/sun/star/script/XTypeConverter.hpp>
64 : #include <com/sun/star/sdbc/XWarningsSupplier.hpp>
65 : #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
66 : #include <com/sun/star/sdbcx/XUsersSupplier.hpp>
67 : #include <com/sun/star/sdbcx/XViewsSupplier.hpp>
68 : #include <com/sun/star/sdbc/XConnection.hpp>
69 :
70 : #include <com/sun/star/container/XNameAccess.hpp>
71 :
72 : #include <rtl/ref.hxx>
73 : #include <rtl/byteseq.hxx>
74 :
75 : #include <salhelper/simplereferenceobject.hxx>
76 :
77 : #include <cppuhelper/weakref.hxx>
78 : #include <cppuhelper/compbase6.hxx>
79 :
80 : #include <libpq-fe.h>
81 : #include "pq_allocator.hxx"
82 :
83 : namespace pq_sdbc_driver
84 : {
85 : #ifdef POSTGRE_TRACE
86 : #define POSTGRE_TRACE( x ) printf( "%s\n" , x )
87 : #else
88 : #define POSTGRE_TRACE(x) ((void)0)
89 : #endif
90 :
91 0 : class RefCountedMutex : public salhelper::SimpleReferenceObject
92 : {
93 : public:
94 : osl::Mutex mutex;
95 : };
96 :
97 : struct ConnectionSettings;
98 :
99 :
100 :
101 : //--------------------------------------------------
102 : // Logging API
103 : //--------------------------------------------------
104 : namespace LogLevel
105 : {
106 : // when you add a loglevel, extend the log function !
107 : static const sal_Int32 NONE = 0;
108 : static const sal_Int32 ERROR = 1;
109 : static const sal_Int32 SQL = 2;
110 : static const sal_Int32 INFO = 3;
111 : static const sal_Int32 DATA = 4;
112 : }
113 : bool isLog( ConnectionSettings *settings, int loglevel );
114 : void log( ConnectionSettings *settings, sal_Int32 level, const rtl::OUString &logString );
115 : void log( ConnectionSettings *settings, sal_Int32 level, const char *str );
116 : //--------------------------------------------------
117 :
118 : class Tables;
119 : class Views;
120 0 : struct ConnectionSettings
121 : {
122 0 : ConnectionSettings() :
123 : encoding( RTL_TEXTENCODING_UTF8),
124 : pConnection(0),
125 : maxNameLen(0),
126 : maxIndexKeys(0),
127 : showSystemColumns( sal_False ),
128 : logFile( 0 ),
129 0 : loglevel( LogLevel::INFO )
130 0 : {}
131 : rtl_TextEncoding encoding;
132 : PGconn *pConnection;
133 : sal_Int32 maxNameLen;
134 : sal_Int32 maxIndexKeys;
135 : ::com::sun::star::uno::Reference< com::sun::star::script::XTypeConverter > tc;
136 : ::com::sun::star::uno::Reference< com::sun::star::container::XNameAccess > tables;
137 : ::com::sun::star::uno::Reference< com::sun::star::container::XNameAccess > users;
138 : ::com::sun::star::uno::Reference< com::sun::star::container::XNameAccess > views;
139 : Tables *pTablesImpl; // needed to implement renaming of tables / views
140 : Views *pViewsImpl; // needed to implement renaming of tables / views
141 : ::rtl::OUString user;
142 : ::rtl::OUString catalog;
143 : sal_Bool showSystemColumns;
144 : FILE *logFile;
145 : sal_Int32 loglevel;
146 : };
147 :
148 : //--------------------------------------------------
149 : typedef cppu::WeakComponentImplHelper6<
150 : com::sun::star::sdbc::XConnection,
151 : com::sun::star::sdbc::XWarningsSupplier,
152 : com::sun::star::lang::XInitialization,
153 : com::sun::star::sdbcx::XTablesSupplier,
154 : com::sun::star::sdbcx::XViewsSupplier,
155 : com::sun::star::sdbcx::XUsersSupplier > ConnectionBase;
156 :
157 : // some types
158 : struct HashByteSequence
159 : {
160 0 : sal_Int32 operator () ( const ::rtl::ByteSequence & seq ) const
161 : {
162 0 : return *(sal_Int32*) seq.getConstArray();
163 : }
164 : };
165 :
166 : typedef ::boost::unordered_map<
167 : ::rtl::ByteSequence,
168 : ::com::sun::star::uno::WeakReference< com::sun::star::sdbc::XCloseable >,
169 : HashByteSequence,
170 : ::std::equal_to< ::rtl::ByteSequence >,
171 : Allocator< std::pair< const ::rtl::ByteSequence,::com::sun::star::uno::WeakReference< com::sun::star::sdbc::XCloseable > > >
172 : > WeakHashMap;
173 : typedef ::std::vector< rtl::OString, Allocator< ::rtl::OString > > OStringVector;
174 :
175 :
176 :
177 : typedef ::boost::unordered_map
178 : <
179 : const sal_Int32,
180 : rtl::OUString,
181 : ::boost::hash< sal_Int32 >,
182 : ::std::equal_to< sal_Int32 >,
183 : Allocator< ::std::pair< sal_Int32, ::rtl::OUString > >
184 : > Int2StringMap;
185 :
186 : class Connection : public ConnectionBase
187 : {
188 : ::com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext > m_ctx;
189 : ::com::sun::star::uno::Reference< com::sun::star::container::XNameAccess > m_typeMap;
190 : ConnectionSettings m_settings;
191 : ::rtl::Reference< RefCountedMutex > m_refMutex;
192 : ::com::sun::star::uno::Reference< com::sun::star::sdbc::XDatabaseMetaData > m_meta;
193 : WeakHashMap m_myStatements;
194 :
195 : private:
196 : void checkClosed()
197 : throw ( com::sun::star::sdbc::SQLException, com::sun::star::uno::RuntimeException );
198 :
199 : public:
200 : Connection(
201 : const rtl::Reference< RefCountedMutex > &refMutex,
202 : const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > & ctx );
203 :
204 : ~Connection( );
205 :
206 : public: // XCloseable
207 : virtual void SAL_CALL close()
208 : throw ( ::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException );
209 :
210 : public: // XConnection
211 :
212 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XStatement > SAL_CALL createStatement( )
213 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) ;
214 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XPreparedStatement > SAL_CALL prepareStatement(
215 : const ::rtl::OUString& sql )
216 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
217 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XPreparedStatement > SAL_CALL prepareCall(
218 : const ::rtl::OUString& sql )
219 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
220 : virtual ::rtl::OUString SAL_CALL nativeSQL( const ::rtl::OUString& sql )
221 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
222 : virtual void SAL_CALL setAutoCommit( sal_Bool autoCommit )
223 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
224 : virtual sal_Bool SAL_CALL getAutoCommit( )
225 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
226 : virtual void SAL_CALL commit( )
227 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
228 : virtual void SAL_CALL rollback( )
229 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
230 : virtual sal_Bool SAL_CALL isClosed( )
231 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
232 : virtual ::com::sun::star::uno::Reference< com::sun::star::sdbc::XDatabaseMetaData > SAL_CALL getMetaData( )
233 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
234 : virtual void SAL_CALL setReadOnly( sal_Bool readOnly )
235 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
236 : virtual sal_Bool SAL_CALL isReadOnly( )
237 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
238 : virtual void SAL_CALL setCatalog( const ::rtl::OUString& catalog )
239 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
240 : virtual ::rtl::OUString SAL_CALL getCatalog( )
241 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
242 : virtual void SAL_CALL setTransactionIsolation( sal_Int32 level )
243 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
244 : virtual sal_Int32 SAL_CALL getTransactionIsolation( )
245 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
246 : virtual ::com::sun::star::uno::Reference< com::sun::star::container::XNameAccess > SAL_CALL getTypeMap( )
247 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
248 : virtual void SAL_CALL setTypeMap(
249 : const ::com::sun::star::uno::Reference< com::sun::star::container::XNameAccess >& typeMap )
250 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
251 :
252 : public: // XWarningsSupplier
253 : virtual ::com::sun::star::uno::Any SAL_CALL getWarnings( )
254 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
255 : virtual void SAL_CALL clearWarnings( )
256 : throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
257 :
258 : public: // XInitialization
259 : virtual void SAL_CALL initialize(
260 : const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aArguments )
261 : throw (com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
262 :
263 : public: // XTablesSupplier
264 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess > SAL_CALL getTables( ) throw (::com::sun::star::uno::RuntimeException);
265 :
266 : public: // XUsersSupplier
267 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess > SAL_CALL getUsers( ) throw (::com::sun::star::uno::RuntimeException);
268 :
269 : public: // XViewsSupplier
270 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess > SAL_CALL getViews( ) throw (::com::sun::star::uno::RuntimeException);
271 :
272 : public:
273 : virtual void SAL_CALL disposing();
274 :
275 : public: // helper function
276 : void removeFromWeakMap( const ::rtl::ByteSequence & seq );
277 : };
278 :
279 : }
280 : #endif
|