2011-11-17 21:29:14 +01:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2011-08-02 16:10:00 +02:00
|
|
|
/*************************************************************************
|
|
|
|
*
|
2011-11-21 11:29:03 +01:00
|
|
|
* Effective License of whole file:
|
2011-08-02 16:10:00 +02:00
|
|
|
*
|
2011-11-21 11:29:03 +01:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License version 2.1, as published by the Free Software Foundation.
|
2011-08-02 16:10:00 +02:00
|
|
|
*
|
2011-11-21 11:29:03 +01:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2011-08-02 16:10:00 +02:00
|
|
|
*
|
2011-11-21 11:29:03 +01:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
|
|
|
* MA 02111-1307 USA
|
2011-08-02 16:10:00 +02:00
|
|
|
*
|
2011-11-21 11:29:03 +01:00
|
|
|
* Parts "Copyright by Sun Microsystems, Inc" prior to August 2011:
|
2011-08-02 16:10:00 +02:00
|
|
|
*
|
2011-11-21 11:29:03 +01:00
|
|
|
* The Contents of this file are made available subject to the terms of
|
|
|
|
* the GNU Lesser General Public License Version 2.1
|
2011-08-02 16:10:00 +02:00
|
|
|
*
|
2011-11-21 11:29:03 +01:00
|
|
|
* Copyright: 2000 by Sun Microsystems, Inc.
|
|
|
|
*
|
|
|
|
* Contributor(s): Joerg Budischewski
|
|
|
|
*
|
|
|
|
* All parts contributed on or after August 2011:
|
|
|
|
*
|
2013-04-24 17:14:03 +01:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2011-11-21 11:29:03 +01:00
|
|
|
*
|
2011-08-02 16:10:00 +02:00
|
|
|
************************************************************************/
|
|
|
|
|
2017-08-31 21:42:24 +02:00
|
|
|
#include <vector>
|
2011-08-02 16:10:00 +02:00
|
|
|
#include <time.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-09-14 14:43:18 +01:00
|
|
|
#include <memory>
|
2011-09-06 23:04:57 +02:00
|
|
|
|
2011-08-02 16:10:00 +02:00
|
|
|
#include "pq_connection.hxx"
|
|
|
|
#include "pq_statement.hxx"
|
|
|
|
#include "pq_preparedstatement.hxx"
|
|
|
|
#include "pq_databasemetadata.hxx"
|
|
|
|
#include "pq_xtables.hxx"
|
|
|
|
#include "pq_xviews.hxx"
|
|
|
|
#include "pq_xusers.hxx"
|
|
|
|
|
|
|
|
#include <rtl/uuid.h>
|
|
|
|
#include <rtl/bootstrap.hxx>
|
2018-07-14 00:48:37 +02:00
|
|
|
#include <sal/log.hxx>
|
2016-09-15 11:35:05 +02:00
|
|
|
#include <o3tl/enumarray.hxx>
|
2011-08-02 16:10:00 +02:00
|
|
|
#include <osl/module.h>
|
|
|
|
|
|
|
|
#include <cppuhelper/implementationentry.hxx>
|
2015-07-13 11:21:57 +09:00
|
|
|
#include <cppuhelper/implbase.hxx>
|
2011-08-02 16:10:00 +02:00
|
|
|
|
|
|
|
#include <com/sun/star/beans/PropertyValue.hpp>
|
2012-08-21 08:07:58 +02:00
|
|
|
#include <com/sun/star/script/Converter.hpp>
|
2017-02-06 17:08:38 +01:00
|
|
|
#include <com/sun/star/sdbc/SQLException.hpp>
|
2011-08-02 16:10:00 +02:00
|
|
|
|
|
|
|
using osl::MutexGuard;
|
|
|
|
|
|
|
|
using com::sun::star::container::XNameAccess;
|
|
|
|
|
|
|
|
using com::sun::star::lang::XComponent;
|
|
|
|
using com::sun::star::lang::IllegalArgumentException;
|
|
|
|
|
2012-08-21 08:07:58 +02:00
|
|
|
using com::sun::star::script::Converter;
|
2011-08-02 16:10:00 +02:00
|
|
|
using com::sun::star::script::XTypeConverter;
|
|
|
|
|
|
|
|
using com::sun::star::uno::RuntimeException;
|
|
|
|
using com::sun::star::uno::Sequence;
|
|
|
|
using com::sun::star::uno::Reference;
|
|
|
|
using com::sun::star::uno::XInterface;
|
|
|
|
using com::sun::star::uno::UNO_QUERY;
|
2019-03-11 11:06:57 +02:00
|
|
|
using com::sun::star::uno::UNO_QUERY_THROW;
|
2011-08-02 16:10:00 +02:00
|
|
|
using com::sun::star::uno::XComponentContext;
|
|
|
|
using com::sun::star::uno::Any;
|
|
|
|
|
|
|
|
using com::sun::star::beans::PropertyValue;
|
|
|
|
|
|
|
|
using com::sun::star::sdbc::XCloseable;
|
|
|
|
using com::sun::star::sdbc::SQLException;
|
|
|
|
using com::sun::star::sdbc::XPreparedStatement;
|
|
|
|
using com::sun::star::sdbc::XStatement;
|
|
|
|
using com::sun::star::sdbc::XDatabaseMetaData;
|
|
|
|
|
|
|
|
namespace pq_sdbc_driver
|
|
|
|
{
|
|
|
|
|
Extend loplugin:external to warn about classes
...following up on 314f15bff08b76bf96acf99141776ef64d2f1355 "Extend
loplugin:external to warn about enums".
Cases where free functions were moved into an unnamed namespace along with a
class, to not break ADL, are in:
filter/source/svg/svgexport.cxx
sc/source/filter/excel/xelink.cxx
sc/source/filter/excel/xilink.cxx
svx/source/sdr/contact/viewobjectcontactofunocontrol.cxx
All other free functions mentioning moved classes appear to be harmless and not
give rise to (silent, even) ADL breakage. (One remaining TODO in
compilerplugins/clang/external.cxx is that derived classes are not covered by
computeAffectedTypes, even though they could also be affected by ADL-breakage---
but don't seem to be in any acutal case across the code base.)
For friend declarations using elaborate type specifiers, like
class C1 {};
class C2 { friend class C1; };
* If C2 (but not C1) is moved into an unnamed namespace, the friend declaration
must be changed to not use an elaborate type specifier (i.e., "friend C1;"; see
C++17 [namespace.memdef]/3: "If the name in a friend declaration is neither
qualified nor a template-id and the declaration is a function or an
elaborated-type-specifier, the lookup to determine whether the entity has been
previously declared shall not consider any scopes outside the innermost
enclosing namespace.")
* If C1 (but not C2) is moved into an unnamed namespace, the friend declaration
must be changed too, see <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71882>
"elaborated-type-specifier friend not looked up in unnamed namespace".
Apart from that, to keep changes simple and mostly mechanical (which should help
avoid regressions), out-of-line definitions of class members have been left in
the enclosing (named) namespace. But explicit specializations of class
templates had to be moved into the unnamed namespace to appease
<https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92598> "explicit specialization of
template from unnamed namespace using unqualified-id in enclosing namespace".
Also, accompanying declarations (of e.g. typedefs or static variables) that
could arguably be moved into the unnamed namespace too have been left alone.
And in some cases, mention of affected types in blacklists in other loplugins
needed to be adapted.
And sc/qa/unit/mark_test.cxx uses a hack of including other .cxx, one of which
is sc/source/core/data/segmenttree.cxx where e.g. ScFlatUInt16SegmentsImpl is
not moved into an unnamed namespace (because it is declared in
sc/inc/segmenttree.hxx), but its base ScFlatSegmentsImpl is. GCC warns about
such combinations with enabled-by-default -Wsubobject-linkage, but "The compiler
doesn’t give this warning for types defined in the main .C file, as those are
unlikely to have multiple definitions."
(<https://gcc.gnu.org/onlinedocs/gcc-9.2.0/gcc/Warning-Options.html>) The
warned-about classes also don't have multiple definitions in the given test, so
disable the warning when including the .cxx.
Change-Id: Ib694094c0d8168be68f8fe90dfd0acbb66a3f1e4
Reviewed-on: https://gerrit.libreoffice.org/83239
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2019-11-19 16:32:49 +01:00
|
|
|
namespace {
|
2011-08-02 16:10:00 +02:00
|
|
|
|
|
|
|
// Helper class for statement lifetime management
|
2016-05-27 12:31:29 +02:00
|
|
|
class ClosableReference : public cppu::WeakImplHelper< css::uno::XReference >
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2016-06-01 16:22:29 +02:00
|
|
|
rtl::Reference<Connection> m_conn;
|
2011-08-02 16:10:00 +02:00
|
|
|
::rtl::ByteSequence m_id;
|
|
|
|
public:
|
|
|
|
ClosableReference( const ::rtl::ByteSequence & id , Connection *that )
|
2011-08-05 12:08:40 +02:00
|
|
|
: m_conn( that ), m_id( id )
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-01-26 12:28:58 +01:00
|
|
|
virtual void SAL_CALL dispose() override
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2016-06-01 16:22:29 +02:00
|
|
|
if( m_conn.is() )
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
m_conn->removeFromWeakMap(m_id);
|
2016-06-01 16:22:29 +02:00
|
|
|
m_conn.clear();
|
2011-08-02 16:10:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
Extend loplugin:external to warn about classes
...following up on 314f15bff08b76bf96acf99141776ef64d2f1355 "Extend
loplugin:external to warn about enums".
Cases where free functions were moved into an unnamed namespace along with a
class, to not break ADL, are in:
filter/source/svg/svgexport.cxx
sc/source/filter/excel/xelink.cxx
sc/source/filter/excel/xilink.cxx
svx/source/sdr/contact/viewobjectcontactofunocontrol.cxx
All other free functions mentioning moved classes appear to be harmless and not
give rise to (silent, even) ADL breakage. (One remaining TODO in
compilerplugins/clang/external.cxx is that derived classes are not covered by
computeAffectedTypes, even though they could also be affected by ADL-breakage---
but don't seem to be in any acutal case across the code base.)
For friend declarations using elaborate type specifiers, like
class C1 {};
class C2 { friend class C1; };
* If C2 (but not C1) is moved into an unnamed namespace, the friend declaration
must be changed to not use an elaborate type specifier (i.e., "friend C1;"; see
C++17 [namespace.memdef]/3: "If the name in a friend declaration is neither
qualified nor a template-id and the declaration is a function or an
elaborated-type-specifier, the lookup to determine whether the entity has been
previously declared shall not consider any scopes outside the innermost
enclosing namespace.")
* If C1 (but not C2) is moved into an unnamed namespace, the friend declaration
must be changed too, see <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71882>
"elaborated-type-specifier friend not looked up in unnamed namespace".
Apart from that, to keep changes simple and mostly mechanical (which should help
avoid regressions), out-of-line definitions of class members have been left in
the enclosing (named) namespace. But explicit specializations of class
templates had to be moved into the unnamed namespace to appease
<https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92598> "explicit specialization of
template from unnamed namespace using unqualified-id in enclosing namespace".
Also, accompanying declarations (of e.g. typedefs or static variables) that
could arguably be moved into the unnamed namespace too have been left alone.
And in some cases, mention of affected types in blacklists in other loplugins
needed to be adapted.
And sc/qa/unit/mark_test.cxx uses a hack of including other .cxx, one of which
is sc/source/core/data/segmenttree.cxx where e.g. ScFlatUInt16SegmentsImpl is
not moved into an unnamed namespace (because it is declared in
sc/inc/segmenttree.hxx), but its base ScFlatSegmentsImpl is. GCC warns about
such combinations with enabled-by-default -Wsubobject-linkage, but "The compiler
doesn’t give this warning for types defined in the main .C file, as those are
unlikely to have multiple definitions."
(<https://gcc.gnu.org/onlinedocs/gcc-9.2.0/gcc/Warning-Options.html>) The
warned-about classes also don't have multiple definitions in the given test, so
disable the warning when including the .cxx.
Change-Id: Ib694094c0d8168be68f8fe90dfd0acbb66a3f1e4
Reviewed-on: https://gerrit.libreoffice.org/83239
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2019-11-19 16:32:49 +01:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:13:19 +02:00
|
|
|
static OUString ConnectionGetImplementationName()
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2019-07-30 17:57:10 +02:00
|
|
|
return "org.openoffice.comp.connectivity.pq.Connection.noext";
|
2011-08-02 16:10:00 +02:00
|
|
|
}
|
2018-09-15 19:13:19 +02:00
|
|
|
static css::uno::Sequence<OUString> ConnectionGetSupportedServiceNames()
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2015-11-06 09:23:33 +02:00
|
|
|
return Sequence< OUString > { "com.sun.star.sdbc.Connection" };
|
2011-08-02 16:10:00 +02:00
|
|
|
}
|
|
|
|
|
2016-09-15 11:35:05 +02:00
|
|
|
static LogLevel readLogLevelFromConfiguration()
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2016-09-15 11:35:05 +02:00
|
|
|
LogLevel nLogLevel = LogLevel::NONE;
|
2011-08-02 16:10:00 +02:00
|
|
|
OUString fileName;
|
2015-01-28 13:50:17 +01:00
|
|
|
osl_getModuleURLFromFunctionAddress(
|
|
|
|
reinterpret_cast<oslGenericFunction>(readLogLevelFromConfiguration), &fileName.pData );
|
2020-02-23 17:36:14 +01:00
|
|
|
fileName = fileName.copy( 0, fileName.lastIndexOf( '/' )+1 ) +
|
Make the "Mac-like" or "canonical" app bundle structure always used on OS X
In other words, only executable files go in the MacOS folder. Dynamic
libraries and bundled frameworks (i.e., LibreOfficePython), and
nothing else, go in the Frameworks folder, and all other files go in
the Resources folder.
Especially, note that Java class files and rc (.ini) files also go in
Resources.
Such an app bundle structure is what Apple strongly suggests one
should use, and it has been hinted that future versions of code
signing and/or Gatekeeper will require such a structure.
There is still some ugliness thanks to traces of the historical
separation of URE from "the office". Like there are two separate
"unorc" files, one for URE, one for the LibreOffice application. IMHO,
this should be cleaned up, but is probably controversial.
(Eek! I now see there are actually *three* unorc files in the app
bundle. Not intentional. Need to fix that later.)
Change-Id: Idcf235038deb5b8e1d061734993e9f31869b7606
2014-09-09 10:44:56 +03:00
|
|
|
#ifdef MACOSX
|
2019-10-08 17:41:51 +02:00
|
|
|
"../Resources/"
|
Make the "Mac-like" or "canonical" app bundle structure always used on OS X
In other words, only executable files go in the MacOS folder. Dynamic
libraries and bundled frameworks (i.e., LibreOfficePython), and
nothing else, go in the Frameworks folder, and all other files go in
the Resources folder.
Especially, note that Java class files and rc (.ini) files also go in
Resources.
Such an app bundle structure is what Apple strongly suggests one
should use, and it has been hinted that future versions of code
signing and/or Gatekeeper will require such a structure.
There is still some ugliness thanks to traces of the historical
separation of URE from "the office". Like there are two separate
"unorc" files, one for URE, one for the LibreOffice application. IMHO,
this should be cleaned up, but is probably controversial.
(Eek! I now see there are actually *three* unorc files in the app
bundle. Not intentional. Need to fix that later.)
Change-Id: Idcf235038deb5b8e1d061734993e9f31869b7606
2014-09-09 10:44:56 +03:00
|
|
|
#endif
|
2019-10-08 17:41:51 +02:00
|
|
|
"postgresql-sdbc.ini";
|
2011-08-02 16:10:00 +02:00
|
|
|
rtl::Bootstrap bootstrapHandle( fileName );
|
|
|
|
|
|
|
|
OUString str;
|
2013-02-01 09:33:19 +01:00
|
|
|
if( bootstrapHandle.getFrom( "PQ_LOGLEVEL", str ) )
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2012-04-06 15:05:52 +02:00
|
|
|
if ( str == "NONE" )
|
2016-09-15 11:35:05 +02:00
|
|
|
nLogLevel = LogLevel::NONE;
|
2012-04-06 15:05:52 +02:00
|
|
|
else if ( str == "ERROR" )
|
2016-09-15 11:35:05 +02:00
|
|
|
nLogLevel = LogLevel::Error;
|
2012-04-06 15:05:52 +02:00
|
|
|
else if ( str == "SQL" )
|
2016-09-15 11:35:05 +02:00
|
|
|
nLogLevel = LogLevel::Sql;
|
2012-04-06 15:05:52 +02:00
|
|
|
else if ( str == "INFO" )
|
2016-09-15 11:35:05 +02:00
|
|
|
nLogLevel = LogLevel::Info;
|
2011-08-02 16:10:00 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf( stderr, "unknown loglevel %s\n",
|
|
|
|
OUStringToOString( str, RTL_TEXTENCODING_UTF8 ).getStr() );
|
|
|
|
}
|
|
|
|
}
|
2016-09-15 11:35:05 +02:00
|
|
|
return nLogLevel;
|
2011-08-02 16:10:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Connection::Connection(
|
2017-06-19 17:06:34 +02:00
|
|
|
const rtl::Reference< comphelper::RefCountedMutex > &refMutex,
|
2016-05-27 12:31:29 +02:00
|
|
|
const css::uno::Reference< css::uno::XComponentContext > & ctx )
|
2017-06-19 17:06:34 +02:00
|
|
|
: ConnectionBase( refMutex->GetMutex() ),
|
2011-08-02 16:10:00 +02:00
|
|
|
m_ctx( ctx ) ,
|
2017-06-19 17:06:34 +02:00
|
|
|
m_xMutex( refMutex )
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2016-09-15 11:35:05 +02:00
|
|
|
m_settings.m_nLogLevel = readLogLevelFromConfiguration();
|
2011-08-02 16:10:00 +02:00
|
|
|
|
2016-09-15 11:35:05 +02:00
|
|
|
if (m_settings.m_nLogLevel != LogLevel::NONE)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
m_settings.logFile = fopen( "sdbc-pqsql.log", "a" );
|
|
|
|
if( m_settings.logFile )
|
|
|
|
{
|
2015-11-10 10:12:07 +01:00
|
|
|
setvbuf( m_settings.logFile, nullptr, _IONBF, 0 );
|
2016-09-15 11:35:05 +02:00
|
|
|
log(&m_settings, m_settings.m_nLogLevel , "set this loglevel");
|
2011-08-02 16:10:00 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Couldn't open sdbc-pqsql.log file\n" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Connection::~Connection()
|
|
|
|
{
|
|
|
|
if( m_settings.pConnection )
|
|
|
|
{
|
|
|
|
PQfinish( m_settings.pConnection );
|
2015-11-10 10:12:07 +01:00
|
|
|
m_settings.pConnection = nullptr;
|
2011-08-02 16:10:00 +02:00
|
|
|
}
|
|
|
|
if( m_settings.logFile )
|
|
|
|
{
|
|
|
|
fclose( m_settings.logFile );
|
2015-11-10 10:12:07 +01:00
|
|
|
m_settings.logFile = nullptr;
|
2011-08-02 16:10:00 +02:00
|
|
|
}
|
|
|
|
}
|
2017-08-31 21:42:24 +02:00
|
|
|
typedef std::vector< css::uno::Reference< css::sdbc::XCloseable > > CloseableVector;
|
2011-08-02 16:10:00 +02:00
|
|
|
|
2017-08-31 21:42:24 +02:00
|
|
|
typedef std::vector< css::uno::Reference< css::lang::XComponent > > DisposeableVector;
|
2011-08-02 16:10:00 +02:00
|
|
|
|
2017-01-26 12:28:58 +01:00
|
|
|
void Connection::close()
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2017-08-31 21:42:24 +02:00
|
|
|
CloseableVector vectorCloseable;
|
|
|
|
DisposeableVector vectorDispose;
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2017-06-19 17:06:34 +02:00
|
|
|
MutexGuard guard( m_xMutex->GetMutex() );
|
2011-08-02 16:10:00 +02:00
|
|
|
// silently ignore, if the connection has been closed already
|
|
|
|
if( m_settings.pConnection )
|
|
|
|
{
|
2016-09-15 11:35:05 +02:00
|
|
|
log(&m_settings, LogLevel::Info, "closing connection");
|
2011-08-02 16:10:00 +02:00
|
|
|
PQfinish( m_settings.pConnection );
|
2015-11-10 10:12:07 +01:00
|
|
|
m_settings.pConnection = nullptr;
|
2011-08-02 16:10:00 +02:00
|
|
|
}
|
|
|
|
|
2017-08-31 21:42:24 +02:00
|
|
|
vectorDispose.push_back( Reference< XComponent > ( m_settings.users, UNO_QUERY ) );
|
|
|
|
vectorDispose.push_back( Reference< XComponent > ( m_settings.tables , UNO_QUERY ) );
|
|
|
|
vectorDispose.push_back( Reference< XComponent > ( m_meta, UNO_QUERY ) );
|
2011-08-02 16:10:00 +02:00
|
|
|
m_meta.clear();
|
|
|
|
m_settings.tables.clear();
|
|
|
|
m_settings.users.clear();
|
|
|
|
|
2018-01-26 17:38:06 +01:00
|
|
|
for (auto const& statement : m_myStatements)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2018-01-26 17:38:06 +01:00
|
|
|
Reference< XCloseable > r = statement.second;
|
2011-08-02 16:10:00 +02:00
|
|
|
if( r.is() )
|
2017-08-31 21:42:24 +02:00
|
|
|
vectorCloseable.push_back( r );
|
2011-08-02 16:10:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// close all created statements
|
2018-01-26 17:38:06 +01:00
|
|
|
for (auto const& elem : vectorCloseable)
|
2019-08-14 20:11:07 +02:00
|
|
|
elem->close();
|
2011-08-02 16:10:00 +02:00
|
|
|
|
|
|
|
// close all created statements
|
2018-01-26 17:38:06 +01:00
|
|
|
for (auto const& elem : vectorDispose)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2018-01-26 17:38:06 +01:00
|
|
|
if( elem.is() )
|
2019-08-14 20:11:07 +02:00
|
|
|
elem->dispose();
|
2011-08-02 16:10:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Connection::removeFromWeakMap( const ::rtl::ByteSequence & id )
|
|
|
|
{
|
|
|
|
// shrink the list !
|
2017-06-19 17:06:34 +02:00
|
|
|
MutexGuard guard( m_xMutex->GetMutex() );
|
2011-08-02 16:10:00 +02:00
|
|
|
WeakHashMap::iterator ii = m_myStatements.find( id );
|
|
|
|
if( ii != m_myStatements.end() )
|
|
|
|
m_myStatements.erase( ii );
|
|
|
|
}
|
|
|
|
|
2017-01-26 12:28:58 +01:00
|
|
|
Reference< XStatement > Connection::createStatement()
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2017-06-19 17:06:34 +02:00
|
|
|
MutexGuard guard( m_xMutex->GetMutex() );
|
2011-08-02 16:10:00 +02:00
|
|
|
checkClosed();
|
|
|
|
|
2017-06-19 17:06:34 +02:00
|
|
|
Statement *stmt = new Statement( m_xMutex, this , &m_settings );
|
2011-08-02 16:10:00 +02:00
|
|
|
Reference< XStatement > ret( stmt );
|
|
|
|
::rtl::ByteSequence id( 16 );
|
2016-04-20 17:15:39 +02:00
|
|
|
rtl_createUuid( reinterpret_cast<sal_uInt8*>(id.getArray()), nullptr, false );
|
2011-08-02 16:10:00 +02:00
|
|
|
m_myStatements[ id ] = Reference< XCloseable > ( stmt );
|
|
|
|
stmt->queryAdapter()->addReference( new ClosableReference( id, this ) );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-04-07 12:06:47 +02:00
|
|
|
Reference< XPreparedStatement > Connection::prepareStatement( const OUString& sql )
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2017-06-19 17:06:34 +02:00
|
|
|
MutexGuard guard( m_xMutex->GetMutex() );
|
2011-08-02 16:10:00 +02:00
|
|
|
checkClosed();
|
|
|
|
|
2016-09-15 15:54:19 +02:00
|
|
|
OString byteSql = OUStringToOString( sql, ConnectionSettings::encoding );
|
2017-06-19 17:06:34 +02:00
|
|
|
PreparedStatement *stmt = new PreparedStatement( m_xMutex, this, &m_settings, byteSql );
|
2011-08-02 16:10:00 +02:00
|
|
|
Reference< XPreparedStatement > ret = stmt;
|
|
|
|
|
|
|
|
::rtl::ByteSequence id( 16 );
|
2016-04-20 17:15:39 +02:00
|
|
|
rtl_createUuid( reinterpret_cast<sal_uInt8*>(id.getArray()), nullptr, false );
|
2011-08-02 16:10:00 +02:00
|
|
|
m_myStatements[ id ] = Reference< XCloseable > ( stmt );
|
|
|
|
stmt->queryAdapter()->addReference( new ClosableReference( id, this ) );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-04-07 12:06:47 +02:00
|
|
|
Reference< XPreparedStatement > Connection::prepareCall( const OUString& )
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
throw SQLException(
|
2014-05-28 12:50:02 +02:00
|
|
|
"pq_driver: Callable statements not supported",
|
2011-08-02 16:10:00 +02:00
|
|
|
Reference< XInterface > (), OUString() , 1, Any() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-07 12:06:47 +02:00
|
|
|
OUString Connection::nativeSQL( const OUString& sql )
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
return sql;
|
|
|
|
}
|
|
|
|
|
2017-01-26 12:28:58 +01:00
|
|
|
void Connection::setAutoCommit( sal_Bool )
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
// UNSUPPORTED
|
|
|
|
}
|
|
|
|
|
2017-01-26 12:28:58 +01:00
|
|
|
sal_Bool Connection::getAutoCommit()
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
// UNSUPPORTED
|
2016-04-20 17:15:39 +02:00
|
|
|
return true;
|
2011-08-02 16:10:00 +02:00
|
|
|
}
|
|
|
|
|
2017-01-26 12:28:58 +01:00
|
|
|
void Connection::commit()
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
// UNSUPPORTED
|
|
|
|
}
|
|
|
|
|
2017-01-26 12:28:58 +01:00
|
|
|
void Connection::rollback()
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
// UNSUPPORTED
|
|
|
|
}
|
|
|
|
|
2017-01-26 12:28:58 +01:00
|
|
|
sal_Bool Connection::isClosed()
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2015-11-10 10:12:07 +01:00
|
|
|
return m_settings.pConnection == nullptr;
|
2011-08-02 16:10:00 +02:00
|
|
|
}
|
|
|
|
|
2011-11-29 17:18:30 +00:00
|
|
|
Reference< XDatabaseMetaData > Connection::getMetaData()
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2017-06-19 17:06:34 +02:00
|
|
|
MutexGuard guard( m_xMutex->GetMutex() );
|
2011-08-02 16:10:00 +02:00
|
|
|
checkClosed();
|
|
|
|
if( ! m_meta.is() )
|
2017-06-19 17:06:34 +02:00
|
|
|
m_meta = new DatabaseMetaData( m_xMutex, this, &m_settings );
|
2011-08-02 16:10:00 +02:00
|
|
|
return m_meta;
|
|
|
|
}
|
|
|
|
|
2017-01-26 12:28:58 +01:00
|
|
|
void Connection::setReadOnly( sal_Bool )
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
// UNSUPPORTED
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-01-26 12:28:58 +01:00
|
|
|
sal_Bool Connection::isReadOnly()
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
// UNSUPPORTED
|
2016-04-20 17:15:39 +02:00
|
|
|
return false;
|
2011-08-02 16:10:00 +02:00
|
|
|
}
|
|
|
|
|
2013-04-07 12:06:47 +02:00
|
|
|
void Connection::setCatalog( const OUString& )
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
// UNSUPPORTED
|
|
|
|
}
|
|
|
|
|
2017-01-26 12:28:58 +01:00
|
|
|
OUString Connection::getCatalog()
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2017-06-19 17:06:34 +02:00
|
|
|
MutexGuard guard( m_xMutex->GetMutex() );
|
2015-11-10 10:12:07 +01:00
|
|
|
if( m_settings.pConnection == nullptr )
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2013-02-01 09:33:19 +01:00
|
|
|
throw SQLException( "pq_connection: connection is closed", *this,
|
2011-08-02 16:10:00 +02:00
|
|
|
OUString(), 1, Any() );
|
|
|
|
}
|
|
|
|
char * p = PQdb(m_settings.pConnection );
|
2016-09-15 15:54:19 +02:00
|
|
|
return OUString( p, strlen(p) , ConnectionSettings::encoding );
|
2011-08-02 16:10:00 +02:00
|
|
|
}
|
|
|
|
|
2011-11-29 17:18:30 +00:00
|
|
|
void Connection::setTransactionIsolation( sal_Int32 )
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
// UNSUPPORTED
|
|
|
|
}
|
|
|
|
|
2017-01-26 12:28:58 +01:00
|
|
|
sal_Int32 Connection::getTransactionIsolation()
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
// UNSUPPORTED
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-26 12:28:58 +01:00
|
|
|
Reference< XNameAccess > Connection::getTypeMap()
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
Reference< XNameAccess > t;
|
|
|
|
{
|
2017-06-19 17:06:34 +02:00
|
|
|
MutexGuard guard( m_xMutex->GetMutex() );
|
2011-08-02 16:10:00 +02:00
|
|
|
t = m_typeMap;
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Connection::setTypeMap( const Reference< XNameAccess >& typeMap )
|
|
|
|
{
|
2017-06-19 17:06:34 +02:00
|
|
|
MutexGuard guard( m_xMutex->GetMutex() );
|
2011-08-02 16:10:00 +02:00
|
|
|
m_typeMap = typeMap;
|
|
|
|
}
|
2017-01-26 12:28:58 +01:00
|
|
|
Any Connection::getWarnings()
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
return Any();
|
|
|
|
}
|
|
|
|
|
2017-01-26 12:28:58 +01:00
|
|
|
void Connection::clearWarnings()
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
Extend loplugin:external to warn about classes
...following up on 314f15bff08b76bf96acf99141776ef64d2f1355 "Extend
loplugin:external to warn about enums".
Cases where free functions were moved into an unnamed namespace along with a
class, to not break ADL, are in:
filter/source/svg/svgexport.cxx
sc/source/filter/excel/xelink.cxx
sc/source/filter/excel/xilink.cxx
svx/source/sdr/contact/viewobjectcontactofunocontrol.cxx
All other free functions mentioning moved classes appear to be harmless and not
give rise to (silent, even) ADL breakage. (One remaining TODO in
compilerplugins/clang/external.cxx is that derived classes are not covered by
computeAffectedTypes, even though they could also be affected by ADL-breakage---
but don't seem to be in any acutal case across the code base.)
For friend declarations using elaborate type specifiers, like
class C1 {};
class C2 { friend class C1; };
* If C2 (but not C1) is moved into an unnamed namespace, the friend declaration
must be changed to not use an elaborate type specifier (i.e., "friend C1;"; see
C++17 [namespace.memdef]/3: "If the name in a friend declaration is neither
qualified nor a template-id and the declaration is a function or an
elaborated-type-specifier, the lookup to determine whether the entity has been
previously declared shall not consider any scopes outside the innermost
enclosing namespace.")
* If C1 (but not C2) is moved into an unnamed namespace, the friend declaration
must be changed too, see <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71882>
"elaborated-type-specifier friend not looked up in unnamed namespace".
Apart from that, to keep changes simple and mostly mechanical (which should help
avoid regressions), out-of-line definitions of class members have been left in
the enclosing (named) namespace. But explicit specializations of class
templates had to be moved into the unnamed namespace to appease
<https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92598> "explicit specialization of
template from unnamed namespace using unqualified-id in enclosing namespace".
Also, accompanying declarations (of e.g. typedefs or static variables) that
could arguably be moved into the unnamed namespace too have been left alone.
And in some cases, mention of affected types in blacklists in other loplugins
needed to be adapted.
And sc/qa/unit/mark_test.cxx uses a hack of including other .cxx, one of which
is sc/source/core/data/segmenttree.cxx where e.g. ScFlatUInt16SegmentsImpl is
not moved into an unnamed namespace (because it is declared in
sc/inc/segmenttree.hxx), but its base ScFlatSegmentsImpl is. GCC warns about
such combinations with enabled-by-default -Wsubobject-linkage, but "The compiler
doesn’t give this warning for types defined in the main .C file, as those are
unlikely to have multiple definitions."
(<https://gcc.gnu.org/onlinedocs/gcc-9.2.0/gcc/Warning-Options.html>) The
warned-about classes also don't have multiple definitions in the given test, so
disable the warning when including the .cxx.
Change-Id: Ib694094c0d8168be68f8fe90dfd0acbb66a3f1e4
Reviewed-on: https://gerrit.libreoffice.org/83239
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2019-11-19 16:32:49 +01:00
|
|
|
namespace {
|
|
|
|
|
2011-09-06 23:04:57 +02:00
|
|
|
class cstr_vector
|
|
|
|
{
|
|
|
|
std::vector<char*> values;
|
|
|
|
std::vector<bool> acquired;
|
|
|
|
public:
|
|
|
|
cstr_vector () : values(), acquired() { values.reserve(8); acquired.reserve(8); }
|
|
|
|
~cstr_vector ()
|
|
|
|
{
|
|
|
|
OSL_ENSURE(values.size() == acquired.size(), "pq_connection: cstr_vector values and acquired size mismatch");
|
2016-03-28 21:42:43 +02:00
|
|
|
std::vector<bool>::const_iterator pa = acquired.begin();
|
2019-03-09 15:54:13 +03:00
|
|
|
for( const auto& v : values )
|
|
|
|
{
|
2011-09-06 23:04:57 +02:00
|
|
|
if (*pa)
|
2019-03-09 15:54:13 +03:00
|
|
|
free(v);
|
|
|
|
++pa;
|
|
|
|
}
|
2011-09-06 23:04:57 +02:00
|
|
|
}
|
|
|
|
void push_back(const char* s, __sal_NoAcquire)
|
|
|
|
{
|
|
|
|
values.push_back(const_cast<char*>(s));
|
|
|
|
acquired.push_back(false);
|
|
|
|
}
|
|
|
|
void push_back(char* s)
|
|
|
|
{
|
|
|
|
values.push_back(s);
|
|
|
|
acquired.push_back(true);
|
|
|
|
}
|
2011-11-21 10:06:34 +01:00
|
|
|
// This const_cast is there for compatibility with PostgreSQL <= 9.1;
|
|
|
|
// PostgreSQL >= 9.2 has the right const qualifiers in the headers
|
|
|
|
// for a return type of "char const*const*".
|
2019-05-22 13:49:30 +02:00
|
|
|
char const** c_array() const { return const_cast <const char**>(values.data()); }
|
2011-09-06 23:04:57 +02:00
|
|
|
};
|
|
|
|
|
Extend loplugin:external to warn about classes
...following up on 314f15bff08b76bf96acf99141776ef64d2f1355 "Extend
loplugin:external to warn about enums".
Cases where free functions were moved into an unnamed namespace along with a
class, to not break ADL, are in:
filter/source/svg/svgexport.cxx
sc/source/filter/excel/xelink.cxx
sc/source/filter/excel/xilink.cxx
svx/source/sdr/contact/viewobjectcontactofunocontrol.cxx
All other free functions mentioning moved classes appear to be harmless and not
give rise to (silent, even) ADL breakage. (One remaining TODO in
compilerplugins/clang/external.cxx is that derived classes are not covered by
computeAffectedTypes, even though they could also be affected by ADL-breakage---
but don't seem to be in any acutal case across the code base.)
For friend declarations using elaborate type specifiers, like
class C1 {};
class C2 { friend class C1; };
* If C2 (but not C1) is moved into an unnamed namespace, the friend declaration
must be changed to not use an elaborate type specifier (i.e., "friend C1;"; see
C++17 [namespace.memdef]/3: "If the name in a friend declaration is neither
qualified nor a template-id and the declaration is a function or an
elaborated-type-specifier, the lookup to determine whether the entity has been
previously declared shall not consider any scopes outside the innermost
enclosing namespace.")
* If C1 (but not C2) is moved into an unnamed namespace, the friend declaration
must be changed too, see <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71882>
"elaborated-type-specifier friend not looked up in unnamed namespace".
Apart from that, to keep changes simple and mostly mechanical (which should help
avoid regressions), out-of-line definitions of class members have been left in
the enclosing (named) namespace. But explicit specializations of class
templates had to be moved into the unnamed namespace to appease
<https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92598> "explicit specialization of
template from unnamed namespace using unqualified-id in enclosing namespace".
Also, accompanying declarations (of e.g. typedefs or static variables) that
could arguably be moved into the unnamed namespace too have been left alone.
And in some cases, mention of affected types in blacklists in other loplugins
needed to be adapted.
And sc/qa/unit/mark_test.cxx uses a hack of including other .cxx, one of which
is sc/source/core/data/segmenttree.cxx where e.g. ScFlatUInt16SegmentsImpl is
not moved into an unnamed namespace (because it is declared in
sc/inc/segmenttree.hxx), but its base ScFlatSegmentsImpl is. GCC warns about
such combinations with enabled-by-default -Wsubobject-linkage, but "The compiler
doesn’t give this warning for types defined in the main .C file, as those are
unlikely to have multiple definitions."
(<https://gcc.gnu.org/onlinedocs/gcc-9.2.0/gcc/Warning-Options.html>) The
warned-about classes also don't have multiple definitions in the given test, so
disable the warning when including the .cxx.
Change-Id: Ib694094c0d8168be68f8fe90dfd0acbb66a3f1e4
Reviewed-on: https://gerrit.libreoffice.org/83239
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2019-11-19 16:32:49 +01:00
|
|
|
}
|
|
|
|
|
2011-09-06 02:06:28 +02:00
|
|
|
static void properties2arrays( const Sequence< PropertyValue > & args,
|
|
|
|
const Reference< XTypeConverter> &tc,
|
|
|
|
rtl_TextEncoding enc,
|
2011-09-06 23:04:57 +02:00
|
|
|
cstr_vector &keywords,
|
|
|
|
cstr_vector &values)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2011-09-06 02:06:28 +02:00
|
|
|
// LEM TODO: can we just blindly take all properties?
|
|
|
|
// I.e. they are prefiltered to have only relevant ones?
|
|
|
|
// Else, at least support all keywords from
|
|
|
|
// http://www.postgresql.org/docs/9.0/interactive/libpq-connect.html
|
2013-04-16 13:30:30 +01:00
|
|
|
|
|
|
|
static const char* keyword_list[] = {
|
|
|
|
"password",
|
|
|
|
"user",
|
|
|
|
"port",
|
|
|
|
"dbname",
|
|
|
|
"connect_timeout",
|
|
|
|
"options",
|
|
|
|
"requiressl"
|
|
|
|
};
|
|
|
|
|
2011-08-05 15:58:37 +02:00
|
|
|
for( int i = 0; i < args.getLength() ; ++i )
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2013-04-16 13:30:30 +01:00
|
|
|
bool append = false;
|
2016-04-21 11:03:55 +02:00
|
|
|
for(const char* j : keyword_list)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2016-04-21 11:03:55 +02:00
|
|
|
if( args[i].Name.equalsIgnoreAsciiCaseAscii( j ))
|
2013-04-16 13:30:30 +01:00
|
|
|
{
|
2016-04-21 11:03:55 +02:00
|
|
|
keywords.push_back( j, SAL_NO_ACQUIRE );
|
2013-04-16 13:30:30 +01:00
|
|
|
append = true;
|
|
|
|
break;
|
|
|
|
}
|
2011-08-02 16:10:00 +02:00
|
|
|
}
|
2013-04-16 13:30:30 +01:00
|
|
|
|
2011-08-02 16:10:00 +02:00
|
|
|
if( append )
|
|
|
|
{
|
|
|
|
OUString value;
|
2015-04-01 08:36:20 +02:00
|
|
|
tc->convertTo( args[i].Value, cppu::UnoType<decltype(value)>::get() ) >>= value;
|
2013-04-07 12:06:47 +02:00
|
|
|
char *v = strdup(OUStringToOString(value, enc).getStr());
|
2011-09-06 02:06:28 +02:00
|
|
|
values.push_back ( v );
|
2011-08-02 16:10:00 +02:00
|
|
|
}
|
2013-04-16 13:30:30 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// ignore for now
|
2018-08-15 12:30:50 +02:00
|
|
|
SAL_WARN("connectivity.postgresql", "sdbc-postgresql: unknown argument '" << args[i].Name << "' having value: " << args[i].Value );
|
2013-04-16 13:30:30 +01:00
|
|
|
}
|
2011-08-02 16:10:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Connection::initialize( const Sequence< Any >& aArguments )
|
|
|
|
{
|
|
|
|
OUString url;
|
|
|
|
Sequence< PropertyValue > args;
|
|
|
|
|
2012-08-21 08:07:58 +02:00
|
|
|
Reference< XTypeConverter > tc( Converter::create(m_ctx) );
|
2011-08-02 16:10:00 +02:00
|
|
|
if( ! tc.is() )
|
|
|
|
{
|
|
|
|
throw RuntimeException(
|
2014-05-23 12:03:21 +02:00
|
|
|
"pq_driver: Couldn't instantiate converter service" );
|
2011-08-02 16:10:00 +02:00
|
|
|
}
|
|
|
|
if( aArguments.getLength() != 2 )
|
|
|
|
{
|
2017-02-01 12:24:07 +02:00
|
|
|
throw IllegalArgumentException(
|
|
|
|
"pq_driver: expected 2 arguments, got " + OUString::number( aArguments.getLength( ) ),
|
|
|
|
Reference< XInterface > () , 0 );
|
2011-08-02 16:10:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( ! (aArguments[0] >>= url) )
|
|
|
|
{
|
2017-02-01 12:24:07 +02:00
|
|
|
throw IllegalArgumentException(
|
|
|
|
"pq_driver: expected string as first argument, got "
|
|
|
|
+ aArguments[0].getValueType().getTypeName(),
|
|
|
|
*this, 0 );
|
2011-08-02 16:10:00 +02:00
|
|
|
}
|
|
|
|
|
2015-04-01 08:36:20 +02:00
|
|
|
tc->convertTo( aArguments[1], cppu::UnoType<decltype(args)>::get() ) >>= args;
|
2011-08-02 16:10:00 +02:00
|
|
|
|
|
|
|
OString o;
|
|
|
|
int nColon = url.indexOf( ':' );
|
|
|
|
if( nColon != -1 )
|
|
|
|
{
|
|
|
|
nColon = url.indexOf( ':' , 1+ nColon );
|
|
|
|
if( nColon != -1 )
|
|
|
|
{
|
2016-09-15 15:54:19 +02:00
|
|
|
o = OUStringToOString( url.getStr()+nColon+1, ConnectionSettings::encoding );
|
2011-08-02 16:10:00 +02:00
|
|
|
}
|
|
|
|
}
|
2011-09-06 02:06:28 +02:00
|
|
|
{
|
2011-09-06 23:04:57 +02:00
|
|
|
cstr_vector keywords;
|
|
|
|
cstr_vector values;
|
2011-08-02 16:10:00 +02:00
|
|
|
|
2011-09-06 02:06:28 +02:00
|
|
|
if ( o.getLength() > 0 )
|
|
|
|
{
|
|
|
|
char *err;
|
2015-09-14 14:43:18 +01:00
|
|
|
std::shared_ptr<PQconninfoOption> oOpts(PQconninfoParse(o.getStr(), &err), PQconninfoFree);
|
2018-10-16 15:23:12 +02:00
|
|
|
if (oOpts == nullptr)
|
2011-09-06 02:06:28 +02:00
|
|
|
{
|
|
|
|
OUString errorMessage;
|
2015-11-10 10:12:07 +01:00
|
|
|
if ( err != nullptr)
|
2011-09-06 02:06:28 +02:00
|
|
|
{
|
2016-09-15 15:54:19 +02:00
|
|
|
errorMessage = OUString( err, strlen(err), ConnectionSettings::encoding );
|
2011-09-06 02:06:28 +02:00
|
|
|
free(err);
|
|
|
|
}
|
|
|
|
else
|
2013-11-04 13:51:21 +02:00
|
|
|
errorMessage = "#no error message#";
|
2011-09-06 02:06:28 +02:00
|
|
|
// HY092 is "Invalid attribute/option identifier."
|
|
|
|
// Just the most likely error; the error might be HY024 "Invalid attribute value".
|
2017-02-01 12:24:07 +02:00
|
|
|
throw SQLException(
|
|
|
|
"Error in database URL '" + url + "':\n" + errorMessage,
|
|
|
|
*this, "HY092", 5, Any() );
|
2011-09-06 02:06:28 +02:00
|
|
|
}
|
|
|
|
|
2015-11-10 10:12:07 +01:00
|
|
|
for ( PQconninfoOption * opt = oOpts.get(); opt->keyword != nullptr; ++opt)
|
2011-09-06 02:06:28 +02:00
|
|
|
{
|
2015-11-10 10:12:07 +01:00
|
|
|
if ( opt->val != nullptr )
|
2011-09-06 02:06:28 +02:00
|
|
|
{
|
2011-09-06 23:04:57 +02:00
|
|
|
keywords.push_back(strdup(opt->keyword));
|
2011-09-06 02:06:28 +02:00
|
|
|
values.push_back(strdup(opt->val));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-15 15:54:19 +02:00
|
|
|
properties2arrays( args , tc, ConnectionSettings::encoding, keywords, values );
|
2015-11-10 10:12:07 +01:00
|
|
|
keywords.push_back(nullptr, SAL_NO_ACQUIRE);
|
|
|
|
values.push_back(nullptr, SAL_NO_ACQUIRE);
|
2011-09-06 02:06:28 +02:00
|
|
|
|
2011-09-06 23:04:57 +02:00
|
|
|
m_settings.pConnection = PQconnectdbParams( keywords.c_array(), values.c_array(), 0 );
|
2011-09-06 02:06:28 +02:00
|
|
|
}
|
2011-08-02 16:10:00 +02:00
|
|
|
if( ! m_settings.pConnection )
|
2014-05-23 12:03:21 +02:00
|
|
|
throw RuntimeException("pq_driver: out of memory" );
|
2011-08-02 16:10:00 +02:00
|
|
|
if( PQstatus( m_settings.pConnection ) == CONNECTION_BAD )
|
|
|
|
{
|
|
|
|
const char * error = PQerrorMessage( m_settings.pConnection );
|
|
|
|
OUString errorMessage( error, strlen( error) , RTL_TEXTENCODING_ASCII_US );
|
|
|
|
PQfinish( m_settings.pConnection );
|
2015-11-10 10:12:07 +01:00
|
|
|
m_settings.pConnection = nullptr;
|
2017-02-01 12:24:07 +02:00
|
|
|
throw SQLException(
|
|
|
|
"Couldn't establish database connection to '" + url + "'\n"
|
|
|
|
+ errorMessage,
|
|
|
|
*this, errorMessage, CONNECTION_BAD, Any() );
|
2011-08-02 16:10:00 +02:00
|
|
|
}
|
|
|
|
PQsetClientEncoding( m_settings.pConnection, "UNICODE" );
|
|
|
|
char *p = PQuser( m_settings.pConnection );
|
|
|
|
m_settings.user = OUString( p, strlen(p), RTL_TEXTENCODING_UTF8);
|
|
|
|
p = PQdb( m_settings.pConnection );
|
|
|
|
m_settings.catalog = OUString( p, strlen(p), RTL_TEXTENCODING_UTF8);
|
|
|
|
m_settings.tc = tc;
|
|
|
|
|
2016-09-15 11:35:05 +02:00
|
|
|
if (isLog(&m_settings, LogLevel::Info))
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2019-10-16 22:53:34 +02:00
|
|
|
log(&m_settings, LogLevel::Info, "connection to '" + url + "' successfully opened");
|
2011-08-02 16:10:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Connection::disposing()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2017-01-26 12:28:58 +01:00
|
|
|
void Connection::checkClosed()
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
if( !m_settings.pConnection )
|
2013-02-01 09:33:19 +01:00
|
|
|
throw SQLException( "pq_connection: Connection already closed",
|
2011-08-02 16:10:00 +02:00
|
|
|
*this, OUString(), 1, Any() );
|
|
|
|
}
|
|
|
|
|
2011-11-29 17:18:30 +00:00
|
|
|
Reference< XNameAccess > Connection::getTables()
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2016-09-15 11:35:05 +02:00
|
|
|
if (isLog(&m_settings, LogLevel::Info))
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2016-09-15 11:35:05 +02:00
|
|
|
log(&m_settings, LogLevel::Info, "Connection::getTables() got called");
|
2011-08-02 16:10:00 +02:00
|
|
|
}
|
2017-06-19 17:06:34 +02:00
|
|
|
MutexGuard guard( m_xMutex->GetMutex() );
|
2011-08-02 16:10:00 +02:00
|
|
|
if( !m_settings.tables.is() )
|
2017-06-19 17:06:34 +02:00
|
|
|
m_settings.tables = Tables::create( m_xMutex, this, &m_settings , &m_settings.pTablesImpl);
|
2011-08-02 16:10:00 +02:00
|
|
|
else
|
|
|
|
// TODO: how to overcome the performance problem ?
|
2019-03-11 11:06:57 +02:00
|
|
|
Reference< css::util::XRefreshable > ( m_settings.tables, UNO_QUERY_THROW )->refresh();
|
2011-08-02 16:10:00 +02:00
|
|
|
return m_settings.tables;
|
|
|
|
}
|
|
|
|
|
2011-11-29 17:18:30 +00:00
|
|
|
Reference< XNameAccess > Connection::getViews()
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2016-09-15 11:35:05 +02:00
|
|
|
if (isLog(&m_settings, LogLevel::Info))
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2016-09-15 11:35:05 +02:00
|
|
|
log(&m_settings, LogLevel::Info, "Connection::getViews() got called");
|
2011-08-02 16:10:00 +02:00
|
|
|
}
|
2017-06-19 17:06:34 +02:00
|
|
|
MutexGuard guard( m_xMutex->GetMutex() );
|
2011-08-02 16:10:00 +02:00
|
|
|
if( !m_settings.views.is() )
|
2017-06-19 17:06:34 +02:00
|
|
|
m_settings.views = Views::create( m_xMutex, this, &m_settings, &(m_settings.pViewsImpl) );
|
2011-08-02 16:10:00 +02:00
|
|
|
else
|
|
|
|
// TODO: how to overcome the performance problem ?
|
2019-03-11 11:06:57 +02:00
|
|
|
Reference< css::util::XRefreshable > ( m_settings.views, UNO_QUERY_THROW )->refresh();
|
2011-08-02 16:10:00 +02:00
|
|
|
return m_settings.views;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-29 17:18:30 +00:00
|
|
|
Reference< XNameAccess > Connection::getUsers()
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2016-09-15 11:35:05 +02:00
|
|
|
if (isLog(&m_settings, LogLevel::Info))
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2016-09-15 11:35:05 +02:00
|
|
|
log(&m_settings, LogLevel::Info, "Connection::getUsers() got called");
|
2011-08-02 16:10:00 +02:00
|
|
|
}
|
|
|
|
|
2017-06-19 17:06:34 +02:00
|
|
|
MutexGuard guard( m_xMutex->GetMutex() );
|
2011-08-02 16:10:00 +02:00
|
|
|
if( !m_settings.users.is() )
|
2017-06-19 17:06:34 +02:00
|
|
|
m_settings.users = Users::create( m_xMutex, this, &m_settings );
|
2011-08-02 16:10:00 +02:00
|
|
|
return m_settings.users;
|
|
|
|
}
|
|
|
|
|
2017-01-19 17:59:45 +01:00
|
|
|
/// @throws Exception
|
2018-09-15 19:13:19 +02:00
|
|
|
static Reference< XInterface > ConnectionCreateInstance(
|
2017-01-26 12:28:58 +01:00
|
|
|
const Reference< XComponentContext > & ctx )
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2017-06-19 17:06:34 +02:00
|
|
|
::rtl::Reference< comphelper::RefCountedMutex > ref = new comphelper::RefCountedMutex;
|
2011-08-02 16:10:00 +02:00
|
|
|
return * new Connection( ref, ctx );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-18 13:40:38 +02:00
|
|
|
bool isLog(ConnectionSettings const *settings, LogLevel nLevel)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2016-09-15 11:35:05 +02:00
|
|
|
return static_cast<int>(settings->m_nLogLevel) >= static_cast<int>(nLevel)
|
|
|
|
&& settings->logFile;
|
2011-08-02 16:10:00 +02:00
|
|
|
}
|
|
|
|
|
2016-09-15 11:35:05 +02:00
|
|
|
void log(ConnectionSettings *settings, LogLevel nLevel, const OUString &logString)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2016-09-15 15:54:19 +02:00
|
|
|
log( settings, nLevel, OUStringToOString( logString, ConnectionSettings::encoding ).getStr() );
|
2011-08-02 16:10:00 +02:00
|
|
|
}
|
2016-09-15 11:35:05 +02:00
|
|
|
void log(ConnectionSettings *settings, LogLevel nLevel, const char *str)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2016-09-15 11:35:05 +02:00
|
|
|
if (isLog(settings, nLevel))
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2016-09-15 11:35:05 +02:00
|
|
|
static const o3tl::enumarray<LogLevel, const char*> strLevel = {"NONE", "ERROR", "SQL", "INFO"};
|
2011-08-02 16:10:00 +02:00
|
|
|
|
2015-11-10 10:12:07 +01:00
|
|
|
time_t t = ::time( nullptr );
|
2011-08-02 16:10:00 +02:00
|
|
|
char *pString;
|
2018-01-18 16:39:59 +01:00
|
|
|
#ifdef _WIN32
|
2011-08-02 16:10:00 +02:00
|
|
|
pString = asctime( localtime( &t ) );
|
|
|
|
#else
|
|
|
|
struct tm timestruc;
|
|
|
|
char timestr[50];
|
|
|
|
memset( timestr, 0 , 50);
|
|
|
|
pString = timestr;
|
|
|
|
::localtime_r( &t , ×truc );
|
|
|
|
asctime_r( ×truc, timestr );
|
|
|
|
#endif
|
|
|
|
for( int i = 0 ; pString[i] ; i ++ )
|
|
|
|
{
|
|
|
|
if( pString[i] <= 13 )
|
|
|
|
{
|
|
|
|
pString[i] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-09-15 11:35:05 +02:00
|
|
|
fprintf(settings->logFile, "%s [%s]: %s\n", pString, strLevel[nLevel], str);
|
2011-08-02 16:10:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-02 10:34:11 +09:00
|
|
|
static const struct cppu::ImplementationEntry g_entries[] =
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
{
|
|
|
|
pq_sdbc_driver::ConnectionCreateInstance, pq_sdbc_driver::ConnectionGetImplementationName,
|
|
|
|
pq_sdbc_driver::ConnectionGetSupportedServiceNames, cppu::createSingleComponentFactory,
|
2015-11-10 10:12:07 +01:00
|
|
|
nullptr , 0
|
2011-08-02 16:10:00 +02:00
|
|
|
},
|
2015-11-10 10:12:07 +01:00
|
|
|
{ nullptr, nullptr, nullptr, nullptr, nullptr, 0 }
|
2011-08-02 16:10:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
|
2017-12-08 15:58:41 +02:00
|
|
|
SAL_DLLPUBLIC_EXPORT void * postgresql_sdbc_impl_component_getFactory(
|
2019-12-18 13:34:12 +02:00
|
|
|
const char * pImplName, void * pServiceManager, void * pRegistryKey )
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
return cppu::component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-02-18 12:11:15 +00:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|