1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * Effective License of whole file:
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.
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.
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,
20 * Parts "Copyright by Sun Microsystems, Inc" prior to August 2011:
22 * The Contents of this file are made available subject to the terms of
23 * the GNU Lesser General Public License Version 2.1
25 * Copyright: 2000 by Sun Microsystems, Inc.
27 * Contributor(s): Joerg Budischewski
29 * All parts contributed on or after August 2011:
31 * Version: MPL 1.1 / GPLv3+ / LGPLv2.1+
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/
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
43 * Major Contributor(s):
44 * [ Copyright (C) 2011 Lionel Elie Mamane <lionel@mamane.lu> ]
46 * All Rights Reserved.
48 * For minor contributions see the git repository.
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.
56 ************************************************************************/
58 #ifndef _PQ_CONNECTION_HXX_
59 #define _PQ_CONNECTION_HXX_
60 #include <boost/unordered_map.hpp>
61 #include <com/sun/star/uno/XComponentContext.hpp>
63 #include <com/sun/star/lang/XInitialization.hpp>
65 #include <com/sun/star/script/XTypeConverter.hpp>
67 #include <com/sun/star/sdbc/XWarningsSupplier.hpp>
68 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
69 #include <com/sun/star/sdbcx/XUsersSupplier.hpp>
70 #include <com/sun/star/sdbcx/XViewsSupplier.hpp>
71 #include <com/sun/star/sdbc/XConnection.hpp>
73 #include <com/sun/star/container/XNameAccess.hpp>
75 #include <rtl/ref.hxx>
76 #include <rtl/byteseq.hxx>
78 #include <salhelper/simplereferenceobject.hxx>
80 #include <cppuhelper/weakref.hxx>
81 #include <cppuhelper/compbase6.hxx>
84 #include "pq_allocator.hxx"
86 namespace pq_sdbc_driver
89 #define POSTGRE_TRACE( x ) printf( "%s\n" , x )
90 #define POSTGRE_TRACE_1( x ,y) printf( "%s %s\n" , x ,y )
92 #define POSTGRE_TRACE(x) ((void)0)
93 #define POSTGRE_TRACE_1(x,y) ((void)0)
96 class RefCountedMutex
: public salhelper::SimpleReferenceObject
102 struct ConnectionSettings
;
106 //--------------------------------------------------
108 //--------------------------------------------------
111 // when you add a loglevel, extend the log function !
112 static const sal_Int32 NONE
= 0;
113 static const sal_Int32 ERROR
= 1;
114 static const sal_Int32 SQL
= 2;
115 static const sal_Int32 INFO
= 3;
116 static const sal_Int32 DATA
= 4;
118 bool isLog( ConnectionSettings
*settings
, int loglevel
);
119 void log( ConnectionSettings
*settings
, sal_Int32 level
, const rtl::OUString
&logString
);
120 void log( ConnectionSettings
*settings
, sal_Int32 level
, const char *str
);
121 //--------------------------------------------------
125 struct ConnectionSettings
127 ConnectionSettings() :
128 encoding( RTL_TEXTENCODING_UTF8
),
132 showSystemColumns( sal_False
),
134 loglevel( LogLevel::INFO
)
136 rtl_TextEncoding encoding
;
138 sal_Int32 maxNameLen
;
139 sal_Int32 maxIndexKeys
;
140 ::com::sun::star::uno::Reference
< com::sun::star::script::XTypeConverter
> tc
;
141 ::com::sun::star::uno::Reference
< com::sun::star::container::XNameAccess
> tables
;
142 ::com::sun::star::uno::Reference
< com::sun::star::container::XNameAccess
> users
;
143 ::com::sun::star::uno::Reference
< com::sun::star::container::XNameAccess
> views
;
144 Tables
*pTablesImpl
; // needed to implement renaming of tables / views
145 Views
*pViewsImpl
; // needed to implement renaming of tables / views
146 ::rtl::OUString user
;
147 ::rtl::OUString catalog
;
148 sal_Bool showSystemColumns
;
153 //--------------------------------------------------
154 typedef cppu::WeakComponentImplHelper6
<
155 com::sun::star::sdbc::XConnection
,
156 com::sun::star::sdbc::XWarningsSupplier
,
157 com::sun::star::lang::XInitialization
,
158 com::sun::star::sdbcx::XTablesSupplier
,
159 com::sun::star::sdbcx::XViewsSupplier
,
160 com::sun::star::sdbcx::XUsersSupplier
> ConnectionBase
;
163 struct HashByteSequence
165 sal_Int32
operator () ( const ::rtl::ByteSequence
& seq
) const
167 return *(sal_Int32
*) seq
.getConstArray();
171 typedef ::boost::unordered_map
<
173 ::com::sun::star::uno::WeakReference
< com::sun::star::sdbc::XCloseable
>,
175 ::std::equal_to
< ::rtl::ByteSequence
>,
176 Allocator
< std::pair
< const ::rtl::ByteSequence
,::com::sun::star::uno::WeakReference
< com::sun::star::sdbc::XCloseable
> > >
178 typedef ::std::vector
< rtl::OString
, Allocator
< ::rtl::OString
> > OStringVector
;
182 typedef ::boost::unordered_map
186 ::boost::hash
< sal_Int32
>,
187 ::std::equal_to
< sal_Int32
>,
188 Allocator
< ::std::pair
< sal_Int32
, ::rtl::OUString
> >
191 class Connection
: public ConnectionBase
193 ::com::sun::star::uno::Reference
< com::sun::star::uno::XComponentContext
> m_ctx
;
194 ::com::sun::star::uno::Reference
< com::sun::star::container::XNameAccess
> m_typeMap
;
195 ConnectionSettings m_settings
;
196 ::rtl::Reference
< RefCountedMutex
> m_refMutex
;
197 ::com::sun::star::uno::Reference
< com::sun::star::sdbc::XDatabaseMetaData
> m_meta
;
198 WeakHashMap m_myStatements
;
202 throw ( com::sun::star::sdbc::SQLException
, com::sun::star::uno::RuntimeException
);
206 const rtl::Reference
< RefCountedMutex
> &refMutex
,
207 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
> & ctx
);
211 public: // XCloseable
212 virtual void SAL_CALL
close()
213 throw ( ::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
);
215 public: // XConnection
217 virtual ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XStatement
> SAL_CALL
createStatement( )
218 throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
) ;
219 virtual ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XPreparedStatement
> SAL_CALL
prepareStatement(
220 const ::rtl::OUString
& sql
)
221 throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
);
222 virtual ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XPreparedStatement
> SAL_CALL
prepareCall(
223 const ::rtl::OUString
& sql
)
224 throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
);
225 virtual ::rtl::OUString SAL_CALL
nativeSQL( const ::rtl::OUString
& sql
)
226 throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
);
227 virtual void SAL_CALL
setAutoCommit( sal_Bool autoCommit
)
228 throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
);
229 virtual sal_Bool SAL_CALL
getAutoCommit( )
230 throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
);
231 virtual void SAL_CALL
commit( )
232 throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
);
233 virtual void SAL_CALL
rollback( )
234 throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
);
235 virtual sal_Bool SAL_CALL
isClosed( )
236 throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
);
237 virtual ::com::sun::star::uno::Reference
< com::sun::star::sdbc::XDatabaseMetaData
> SAL_CALL
getMetaData( )
238 throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
);
239 virtual void SAL_CALL
setReadOnly( sal_Bool readOnly
)
240 throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
);
241 virtual sal_Bool SAL_CALL
isReadOnly( )
242 throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
);
243 virtual void SAL_CALL
setCatalog( const ::rtl::OUString
& catalog
)
244 throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
);
245 virtual ::rtl::OUString SAL_CALL
getCatalog( )
246 throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
);
247 virtual void SAL_CALL
setTransactionIsolation( sal_Int32 level
)
248 throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
);
249 virtual sal_Int32 SAL_CALL
getTransactionIsolation( )
250 throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
);
251 virtual ::com::sun::star::uno::Reference
< com::sun::star::container::XNameAccess
> SAL_CALL
getTypeMap( )
252 throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
);
253 virtual void SAL_CALL
setTypeMap(
254 const ::com::sun::star::uno::Reference
< com::sun::star::container::XNameAccess
>& typeMap
)
255 throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
);
257 public: // XWarningsSupplier
258 virtual ::com::sun::star::uno::Any SAL_CALL
getWarnings( )
259 throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
);
260 virtual void SAL_CALL
clearWarnings( )
261 throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
);
263 public: // XInitialization
264 virtual void SAL_CALL
initialize(
265 const ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
>& aArguments
)
266 throw (com::sun::star::uno::Exception
, ::com::sun::star::uno::RuntimeException
);
268 public: // XTablesSupplier
269 virtual ::com::sun::star::uno::Reference
< ::com::sun::star::container::XNameAccess
> SAL_CALL
getTables( ) throw (::com::sun::star::uno::RuntimeException
);
271 public: // XUsersSupplier
272 virtual ::com::sun::star::uno::Reference
< ::com::sun::star::container::XNameAccess
> SAL_CALL
getUsers( ) throw (::com::sun::star::uno::RuntimeException
);
274 public: // XViewsSupplier
275 virtual ::com::sun::star::uno::Reference
< ::com::sun::star::container::XNameAccess
> SAL_CALL
getViews( ) throw (::com::sun::star::uno::RuntimeException
);
278 virtual void SAL_CALL
disposing();
280 public: // helper function
281 void removeFromWeakMap( const ::rtl::ByteSequence
& seq
);