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
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
#include <list>
|
|
|
|
#include <time.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2011-09-06 23:04:57 +02:00
|
|
|
#include <boost/shared_ptr.hpp>
|
|
|
|
|
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_xcontainer.hxx"
|
|
|
|
#include "pq_statics.hxx"
|
|
|
|
#include "pq_xtables.hxx"
|
|
|
|
#include "pq_xviews.hxx"
|
|
|
|
#include "pq_xusers.hxx"
|
|
|
|
|
|
|
|
#include <rtl/ustrbuf.hxx>
|
|
|
|
#include <rtl/strbuf.hxx>
|
|
|
|
#include <rtl/uuid.h>
|
|
|
|
#include <rtl/bootstrap.hxx>
|
|
|
|
#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>
|
2011-08-02 16:10:00 +02:00
|
|
|
#include <com/sun/star/sdbc/XRow.hpp>
|
|
|
|
|
|
|
|
using osl::MutexGuard;
|
|
|
|
|
|
|
|
using com::sun::star::container::XNameAccess;
|
|
|
|
|
|
|
|
using com::sun::star::lang::XComponent;
|
|
|
|
using com::sun::star::lang::XInitialization;
|
|
|
|
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::Exception;
|
|
|
|
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;
|
|
|
|
using com::sun::star::uno::XComponentContext;
|
|
|
|
using com::sun::star::uno::Any;
|
|
|
|
using com::sun::star::uno::makeAny;
|
|
|
|
|
|
|
|
using com::sun::star::beans::PropertyValue;
|
|
|
|
using com::sun::star::beans::XPropertySet;
|
|
|
|
|
|
|
|
using com::sun::star::sdbc::XConnection;
|
|
|
|
using com::sun::star::sdbc::XResultSet;
|
|
|
|
using com::sun::star::sdbc::XRow;
|
|
|
|
using com::sun::star::sdbc::XCloseable;
|
|
|
|
using com::sun::star::sdbc::SQLException;
|
|
|
|
using com::sun::star::sdbc::XWarningsSupplier;
|
|
|
|
using com::sun::star::sdbc::XPreparedStatement;
|
|
|
|
using com::sun::star::sdbc::XStatement;
|
|
|
|
using com::sun::star::sdbc::XDatabaseMetaData;
|
|
|
|
|
|
|
|
namespace pq_sdbc_driver
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2014-02-25 17:59:09 +01:00
|
|
|
|
2011-08-02 16:10:00 +02:00
|
|
|
// Helper class for statement lifetime management
|
2015-07-13 11:21:57 +09:00
|
|
|
class ClosableReference : public cppu::WeakImplHelper< com::sun::star::uno::XReference >
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
Connection *m_conn;
|
|
|
|
::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
|
|
|
{
|
|
|
|
that->acquire();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~ClosableReference()
|
|
|
|
{
|
|
|
|
if( m_conn )
|
|
|
|
m_conn->release();
|
|
|
|
}
|
|
|
|
|
2014-03-26 16:37:00 +01:00
|
|
|
virtual void SAL_CALL dispose() throw (std::exception) SAL_OVERRIDE
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
if( m_conn )
|
|
|
|
{
|
|
|
|
m_conn->removeFromWeakMap(m_id);
|
|
|
|
m_conn->release();
|
|
|
|
m_conn = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-08-05 15:58:37 +02:00
|
|
|
OUString ConnectionGetImplementationName()
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2013-03-19 09:22:44 +01:00
|
|
|
return OUString( "org.openoffice.comp.connectivity.pq.Connection.noext" );
|
2011-08-02 16:10:00 +02:00
|
|
|
}
|
2015-04-14 12:44:47 +02:00
|
|
|
com::sun::star::uno::Sequence<OUString> ConnectionGetSupportedServiceNames()
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2013-03-19 11:39:07 +01:00
|
|
|
OUString serv( "com.sun.star.sdbc.Connection" );
|
2011-08-02 16:10:00 +02:00
|
|
|
return Sequence< OUString> (&serv,1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static sal_Int32 readLogLevelFromConfiguration()
|
|
|
|
{
|
|
|
|
sal_Int32 loglevel = LogLevel::NONE;
|
|
|
|
OUString fileName;
|
2015-01-28 13:50:17 +01:00
|
|
|
osl_getModuleURLFromFunctionAddress(
|
|
|
|
reinterpret_cast<oslGenericFunction>(readLogLevelFromConfiguration), &fileName.pData );
|
2013-11-04 13:51:21 +02:00
|
|
|
fileName = fileName.copy( 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
|
|
|
|
fileName += "../Resources/";
|
|
|
|
#endif
|
2013-06-29 14:42:54 +02:00
|
|
|
fileName += "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" )
|
2011-08-02 16:10:00 +02:00
|
|
|
loglevel = LogLevel::NONE;
|
2012-04-06 15:05:52 +02:00
|
|
|
else if ( str == "ERROR" )
|
2011-08-02 16:10:00 +02:00
|
|
|
loglevel = LogLevel::ERROR;
|
2012-04-06 15:05:52 +02:00
|
|
|
else if ( str == "SQL" )
|
2011-08-02 16:10:00 +02:00
|
|
|
loglevel = LogLevel::SQL;
|
2012-04-06 15:05:52 +02:00
|
|
|
else if ( str == "INFO" )
|
2012-02-11 23:15:47 +09:00
|
|
|
loglevel = LogLevel::INFO;
|
2011-08-02 16:10:00 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf( stderr, "unknown loglevel %s\n",
|
|
|
|
OUStringToOString( str, RTL_TEXTENCODING_UTF8 ).getStr() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return loglevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
Connection::Connection(
|
|
|
|
const rtl::Reference< RefCountedMutex > &refMutex,
|
|
|
|
const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > & ctx )
|
|
|
|
: ConnectionBase( refMutex->mutex ),
|
|
|
|
m_ctx( ctx ) ,
|
|
|
|
m_refMutex( refMutex )
|
|
|
|
{
|
|
|
|
m_settings.loglevel = readLogLevelFromConfiguration();
|
|
|
|
|
|
|
|
if( m_settings.loglevel > LogLevel::NONE )
|
|
|
|
{
|
|
|
|
m_settings.logFile = fopen( "sdbc-pqsql.log", "a" );
|
|
|
|
if( m_settings.logFile )
|
|
|
|
{
|
|
|
|
setvbuf( m_settings.logFile, 0, _IONBF, 0 );
|
|
|
|
log( &m_settings, m_settings.loglevel , "set this loglevel" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Couldn't open sdbc-pqsql.log file\n" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Connection::~Connection()
|
|
|
|
{
|
|
|
|
POSTGRE_TRACE( "dtor connection" );
|
|
|
|
if( m_settings.pConnection )
|
|
|
|
{
|
|
|
|
PQfinish( m_settings.pConnection );
|
|
|
|
m_settings.pConnection = 0;
|
|
|
|
}
|
|
|
|
if( m_settings.logFile )
|
|
|
|
{
|
|
|
|
fclose( m_settings.logFile );
|
|
|
|
m_settings.logFile = 0;
|
|
|
|
}
|
|
|
|
}
|
2014-05-15 14:23:18 +01:00
|
|
|
typedef ::std::list< ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XCloseable > > CloseableList;
|
2011-08-02 16:10:00 +02:00
|
|
|
|
2014-05-15 14:23:18 +01:00
|
|
|
typedef ::std::list< ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > > DisposeableList;
|
2011-08-02 16:10:00 +02:00
|
|
|
|
2014-02-25 21:31:58 +01:00
|
|
|
void Connection::close() throw ( SQLException, RuntimeException, std::exception )
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
CloseableList lst;
|
|
|
|
DisposeableList lstDispose;
|
|
|
|
{
|
|
|
|
MutexGuard guard( m_refMutex->mutex );
|
|
|
|
// silently ignore, if the connection has been closed already
|
|
|
|
if( m_settings.pConnection )
|
|
|
|
{
|
|
|
|
log( &m_settings, LogLevel::INFO, "closing connection" );
|
|
|
|
PQfinish( m_settings.pConnection );
|
|
|
|
m_settings.pConnection = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
lstDispose.push_back( Reference< XComponent > ( m_settings.users, UNO_QUERY ) );
|
|
|
|
lstDispose.push_back( Reference< XComponent > ( m_settings.tables , UNO_QUERY ) );
|
|
|
|
lstDispose.push_back( Reference< XComponent > ( m_meta, UNO_QUERY ) );
|
|
|
|
m_meta.clear();
|
|
|
|
m_settings.tables.clear();
|
|
|
|
m_settings.users.clear();
|
|
|
|
|
|
|
|
for( WeakHashMap::iterator ii = m_myStatements.begin() ;
|
|
|
|
ii != m_myStatements.end() ;
|
|
|
|
++ii )
|
|
|
|
{
|
|
|
|
Reference< XCloseable > r = ii->second;
|
|
|
|
if( r.is() )
|
|
|
|
lst.push_back( r );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// close all created statements
|
|
|
|
for( CloseableList::iterator ii = lst.begin(); ii != lst.end() ; ++ii )
|
|
|
|
ii->get()->close();
|
|
|
|
|
|
|
|
// close all created statements
|
|
|
|
for( DisposeableList::iterator iiDispose = lstDispose.begin();
|
|
|
|
iiDispose != lstDispose.end() ; ++iiDispose )
|
|
|
|
{
|
|
|
|
if( iiDispose->is() )
|
|
|
|
iiDispose->get()->dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Connection::removeFromWeakMap( const ::rtl::ByteSequence & id )
|
|
|
|
{
|
|
|
|
// shrink the list !
|
|
|
|
MutexGuard guard( m_refMutex->mutex );
|
|
|
|
WeakHashMap::iterator ii = m_myStatements.find( id );
|
|
|
|
if( ii != m_myStatements.end() )
|
|
|
|
m_myStatements.erase( ii );
|
|
|
|
}
|
|
|
|
|
2014-02-25 21:31:58 +01:00
|
|
|
Reference< XStatement > Connection::createStatement() throw (SQLException, RuntimeException, std::exception)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
MutexGuard guard( m_refMutex->mutex );
|
|
|
|
checkClosed();
|
|
|
|
|
|
|
|
Statement *stmt = new Statement( m_refMutex, this , &m_settings );
|
|
|
|
Reference< XStatement > ret( stmt );
|
|
|
|
::rtl::ByteSequence id( 16 );
|
2015-01-17 18:17:00 +01:00
|
|
|
rtl_createUuid( reinterpret_cast<sal_uInt8*>(id.getArray()), 0, sal_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 )
|
2014-02-25 21:31:58 +01:00
|
|
|
throw (SQLException, RuntimeException, std::exception)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
MutexGuard guard( m_refMutex->mutex );
|
|
|
|
checkClosed();
|
|
|
|
|
2013-04-07 12:06:47 +02:00
|
|
|
OString byteSql = OUStringToOString( sql, m_settings.encoding );
|
2011-08-02 16:10:00 +02:00
|
|
|
PreparedStatement *stmt = new PreparedStatement( m_refMutex, this, &m_settings, byteSql );
|
|
|
|
Reference< XPreparedStatement > ret = stmt;
|
|
|
|
|
|
|
|
::rtl::ByteSequence id( 16 );
|
2015-01-17 18:17:00 +01:00
|
|
|
rtl_createUuid( reinterpret_cast<sal_uInt8*>(id.getArray()), 0, sal_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& )
|
2014-02-25 21:31:58 +01:00
|
|
|
throw (SQLException, RuntimeException, std::exception)
|
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 )
|
2014-02-25 21:31:58 +01:00
|
|
|
throw (SQLException, RuntimeException, std::exception)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
return sql;
|
|
|
|
}
|
|
|
|
|
2014-02-25 21:31:58 +01:00
|
|
|
void Connection::setAutoCommit( sal_Bool ) throw (SQLException, RuntimeException, std::exception)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
// UNSUPPORTED
|
|
|
|
}
|
|
|
|
|
2014-02-25 21:31:58 +01:00
|
|
|
sal_Bool Connection::getAutoCommit() throw (SQLException, RuntimeException, std::exception)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
// UNSUPPORTED
|
|
|
|
return sal_True;
|
|
|
|
}
|
|
|
|
|
2014-02-25 21:31:58 +01:00
|
|
|
void Connection::commit() throw (SQLException, RuntimeException, std::exception)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
// UNSUPPORTED
|
|
|
|
}
|
|
|
|
|
2014-02-25 21:31:58 +01:00
|
|
|
void Connection::rollback() throw (SQLException, RuntimeException, std::exception)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
// UNSUPPORTED
|
|
|
|
}
|
|
|
|
|
2014-02-25 21:31:58 +01:00
|
|
|
sal_Bool Connection::isClosed() throw (SQLException, RuntimeException, std::exception)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
return m_settings.pConnection == 0;
|
|
|
|
}
|
|
|
|
|
2011-11-29 17:18:30 +00:00
|
|
|
Reference< XDatabaseMetaData > Connection::getMetaData()
|
2014-02-25 21:31:58 +01:00
|
|
|
throw (SQLException, RuntimeException, std::exception)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
MutexGuard guard( m_refMutex->mutex );
|
|
|
|
checkClosed();
|
|
|
|
if( ! m_meta.is() )
|
|
|
|
m_meta = new DatabaseMetaData( m_refMutex, this, &m_settings );
|
|
|
|
return m_meta;
|
|
|
|
}
|
|
|
|
|
2014-02-25 21:31:58 +01:00
|
|
|
void Connection::setReadOnly( sal_Bool ) throw (SQLException, RuntimeException, std::exception)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
// UNSUPPORTED
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-02-25 21:31:58 +01:00
|
|
|
sal_Bool Connection::isReadOnly() throw (SQLException, RuntimeException, std::exception)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
// UNSUPPORTED
|
|
|
|
return sal_False;
|
|
|
|
}
|
|
|
|
|
2013-04-07 12:06:47 +02:00
|
|
|
void Connection::setCatalog( const OUString& )
|
2014-02-25 21:31:58 +01:00
|
|
|
throw (SQLException, RuntimeException, std::exception)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
// UNSUPPORTED
|
|
|
|
}
|
|
|
|
|
2014-02-25 21:31:58 +01:00
|
|
|
OUString Connection::getCatalog() throw (SQLException, RuntimeException, std::exception)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2012-08-30 23:04:22 +02:00
|
|
|
MutexGuard guard( m_refMutex->mutex );
|
2011-08-02 16:10:00 +02:00
|
|
|
if( m_settings.pConnection == 0 )
|
|
|
|
{
|
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 );
|
|
|
|
return OUString( p, strlen(p) , m_settings.encoding );
|
|
|
|
}
|
|
|
|
|
2011-11-29 17:18:30 +00:00
|
|
|
void Connection::setTransactionIsolation( sal_Int32 )
|
2014-02-25 21:31:58 +01:00
|
|
|
throw (SQLException, RuntimeException, std::exception)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
// UNSUPPORTED
|
|
|
|
}
|
|
|
|
|
2014-02-25 21:31:58 +01:00
|
|
|
sal_Int32 Connection::getTransactionIsolation() throw (SQLException, RuntimeException, std::exception)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
// UNSUPPORTED
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-25 21:31:58 +01:00
|
|
|
Reference< XNameAccess > Connection::getTypeMap() throw (SQLException, RuntimeException, std::exception)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
Reference< XNameAccess > t;
|
|
|
|
{
|
|
|
|
MutexGuard guard( m_refMutex->mutex );
|
|
|
|
t = m_typeMap;
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Connection::setTypeMap( const Reference< XNameAccess >& typeMap )
|
2014-02-25 21:31:58 +01:00
|
|
|
throw (SQLException, RuntimeException, std::exception)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
MutexGuard guard( m_refMutex->mutex );
|
|
|
|
m_typeMap = typeMap;
|
|
|
|
}
|
2014-02-25 21:31:58 +01:00
|
|
|
Any Connection::getWarnings() throw (SQLException, RuntimeException, std::exception)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
return Any();
|
|
|
|
}
|
|
|
|
|
2014-02-25 21:31:58 +01:00
|
|
|
void Connection::clearWarnings() throw (SQLException, RuntimeException, std::exception)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
std::vector<char*>::iterator pv = values.begin();
|
|
|
|
std::vector<bool>::iterator pa = acquired.begin();
|
|
|
|
const std::vector<char*>::iterator pve = values.end();
|
|
|
|
for( ; pv < pve ; ++pv, ++pa )
|
|
|
|
if (*pa)
|
|
|
|
free(*pv);
|
|
|
|
}
|
|
|
|
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*".
|
2011-09-06 23:04:57 +02:00
|
|
|
char const** c_array() const { return const_cast <const char**>(&values[0]); }
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
for( size_t j = 0; j < SAL_N_ELEMENTS( keyword_list ); j++)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
2013-04-16 13:30:30 +01:00
|
|
|
if( args[i].Name.equalsIgnoreAsciiCaseAscii( keyword_list[j] ))
|
|
|
|
{
|
|
|
|
keywords.push_back( keyword_list[j], SAL_NO_ACQUIRE );
|
|
|
|
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
|
|
|
|
OSL_TRACE("sdbc-postgresql: unknown argument '%s'", OUStringToOString( args[i].Name, RTL_TEXTENCODING_UTF8 ).getStr() );
|
|
|
|
}
|
2011-08-02 16:10:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Connection::initialize( const Sequence< Any >& aArguments )
|
2014-02-25 21:31:58 +01:00
|
|
|
throw (Exception, RuntimeException, std::exception)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
OUStringBuffer buf(128);
|
2015-08-31 08:04:12 +02:00
|
|
|
buf.append( "pq_driver: expected 2 arguments, got " );
|
2011-08-02 16:10:00 +02:00
|
|
|
buf.append( aArguments.getLength( ) );
|
|
|
|
throw IllegalArgumentException(buf.makeStringAndClear(), Reference< XInterface > () , 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ! (aArguments[0] >>= url) )
|
|
|
|
{
|
|
|
|
OUStringBuffer buf(128);
|
2015-08-31 08:04:12 +02:00
|
|
|
buf.append( "pq_driver: expected string as first argument, got " );
|
2011-08-02 16:10:00 +02:00
|
|
|
buf.append( aArguments[0].getValueType().getTypeName() );
|
|
|
|
throw IllegalArgumentException( buf.makeStringAndClear() , *this, 0 );
|
|
|
|
}
|
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
o = OUStringToOString( url.getStr()+nColon+1, m_settings.encoding );
|
|
|
|
}
|
|
|
|
}
|
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;
|
2011-09-06 23:04:57 +02:00
|
|
|
boost::shared_ptr<PQconninfoOption> oOpts(PQconninfoParse(o.getStr(), &err), PQconninfoFree);
|
|
|
|
if ( oOpts.get() == NULL )
|
2011-09-06 02:06:28 +02:00
|
|
|
{
|
|
|
|
OUString errorMessage;
|
|
|
|
if ( err != NULL)
|
|
|
|
{
|
|
|
|
errorMessage = OUString( err, strlen(err), m_settings.encoding );
|
|
|
|
free(err);
|
|
|
|
}
|
|
|
|
else
|
2013-11-04 13:51:21 +02:00
|
|
|
errorMessage = "#no error message#";
|
2011-09-06 02:06:28 +02:00
|
|
|
OUStringBuffer buf( 128 );
|
2015-08-31 08:04:12 +02:00
|
|
|
buf.append( "Error in database URL '" );
|
2011-09-06 02:06:28 +02:00
|
|
|
buf.append( url );
|
2015-08-31 08:04:12 +02:00
|
|
|
buf.append( "':\n" );
|
2011-09-06 02:06:28 +02:00
|
|
|
buf.append( errorMessage );
|
|
|
|
// HY092 is "Invalid attribute/option identifier."
|
|
|
|
// Just the most likely error; the error might be HY024 "Invalid attribute value".
|
2013-03-19 09:22:44 +01:00
|
|
|
throw SQLException( buf.makeStringAndClear(), *this, OUString("HY092"), 5, Any() );
|
2011-09-06 02:06:28 +02:00
|
|
|
}
|
|
|
|
|
2011-09-06 23:04:57 +02:00
|
|
|
for ( PQconninfoOption * opt = oOpts.get(); opt->keyword != NULL; ++opt)
|
2011-09-06 02:06:28 +02:00
|
|
|
{
|
|
|
|
if ( opt->val != NULL )
|
|
|
|
{
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
properties2arrays( args , tc, m_settings.encoding, keywords, values );
|
2011-09-06 23:04:57 +02:00
|
|
|
keywords.push_back(NULL, SAL_NO_ACQUIRE);
|
|
|
|
values.push_back(NULL, 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 )
|
|
|
|
{
|
|
|
|
OUStringBuffer buf( 128 );
|
|
|
|
|
|
|
|
const char * error = PQerrorMessage( m_settings.pConnection );
|
|
|
|
OUString errorMessage( error, strlen( error) , RTL_TEXTENCODING_ASCII_US );
|
2015-08-31 08:04:12 +02:00
|
|
|
buf.append( "Couldn't establish database connection to '" );
|
2011-08-02 16:10:00 +02:00
|
|
|
buf.append( url );
|
2015-08-31 08:04:12 +02:00
|
|
|
buf.append( "'\n" );
|
2011-08-02 16:10:00 +02:00
|
|
|
buf.append( errorMessage );
|
|
|
|
PQfinish( m_settings.pConnection );
|
|
|
|
m_settings.pConnection = 0;
|
|
|
|
throw SQLException( buf.makeStringAndClear(), *this, errorMessage, CONNECTION_BAD, Any() );
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
|
|
|
if( isLog( &m_settings, LogLevel::INFO ) )
|
|
|
|
{
|
|
|
|
OUStringBuffer buf( 128 );
|
2015-08-31 08:04:12 +02:00
|
|
|
buf.append( "connection to '" );
|
2011-08-02 16:10:00 +02:00
|
|
|
buf.append( url );
|
2015-08-31 08:04:12 +02:00
|
|
|
buf.append( "' successfully opened" );
|
2011-08-02 16:10:00 +02:00
|
|
|
log( &m_settings, LogLevel::INFO, buf.makeStringAndClear() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Connection::disposing()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Connection::checkClosed() throw ( SQLException, RuntimeException )
|
|
|
|
{
|
|
|
|
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()
|
2014-02-25 21:31:58 +01:00
|
|
|
throw (::com::sun::star::uno::RuntimeException, std::exception)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
if( isLog( &m_settings, LogLevel::INFO ) )
|
|
|
|
{
|
|
|
|
log( &m_settings, LogLevel::INFO, "Connection::getTables() got called" );
|
|
|
|
}
|
|
|
|
MutexGuard guard( m_refMutex->mutex );
|
|
|
|
if( !m_settings.tables.is() )
|
|
|
|
m_settings.tables = Tables::create( m_refMutex, this, &m_settings , &m_settings.pTablesImpl);
|
|
|
|
else
|
|
|
|
// TODO: how to overcome the performance problem ?
|
|
|
|
Reference< com::sun::star::util::XRefreshable > ( m_settings.tables, UNO_QUERY )->refresh();
|
|
|
|
return m_settings.tables;
|
|
|
|
}
|
|
|
|
|
2011-11-29 17:18:30 +00:00
|
|
|
Reference< XNameAccess > Connection::getViews()
|
2014-02-25 21:31:58 +01:00
|
|
|
throw (::com::sun::star::uno::RuntimeException, std::exception)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
if( isLog( &m_settings, LogLevel::INFO ) )
|
|
|
|
{
|
|
|
|
log( &m_settings, LogLevel::INFO, "Connection::getViews() got called" );
|
|
|
|
}
|
|
|
|
MutexGuard guard( m_refMutex->mutex );
|
|
|
|
if( !m_settings.views.is() )
|
|
|
|
m_settings.views = Views::create( m_refMutex, this, &m_settings, &(m_settings.pViewsImpl) );
|
|
|
|
else
|
|
|
|
// TODO: how to overcome the performance problem ?
|
|
|
|
Reference< com::sun::star::util::XRefreshable > ( m_settings.views, UNO_QUERY )->refresh();
|
|
|
|
return m_settings.views;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-11-29 17:18:30 +00:00
|
|
|
Reference< XNameAccess > Connection::getUsers()
|
2014-02-25 21:31:58 +01:00
|
|
|
throw (::com::sun::star::uno::RuntimeException, std::exception)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
if( isLog( &m_settings, LogLevel::INFO ) )
|
|
|
|
{
|
|
|
|
log( &m_settings, LogLevel::INFO, "Connection::getUsers() got called" );
|
|
|
|
}
|
|
|
|
|
|
|
|
MutexGuard guard( m_refMutex->mutex );
|
|
|
|
if( !m_settings.users.is() )
|
|
|
|
m_settings.users = Users::create( m_refMutex, this, &m_settings );
|
|
|
|
return m_settings.users;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Reference< XInterface > ConnectionCreateInstance(
|
2015-06-29 09:14:47 +01:00
|
|
|
const Reference< XComponentContext > & ctx ) throw (Exception, std::exception)
|
2011-08-02 16:10:00 +02:00
|
|
|
{
|
|
|
|
::rtl::Reference< RefCountedMutex > ref = new RefCountedMutex();
|
|
|
|
return * new Connection( ref, ctx );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool isLog( ConnectionSettings *settings, int loglevel )
|
|
|
|
{
|
|
|
|
return settings->loglevel >= loglevel && settings->logFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
void log( ConnectionSettings *settings, sal_Int32 level, const OUString &logString )
|
|
|
|
{
|
|
|
|
log( settings, level, OUStringToOString( logString, settings->encoding ).getStr() );
|
|
|
|
}
|
|
|
|
void log( ConnectionSettings *settings, sal_Int32 level, const char *str )
|
|
|
|
{
|
|
|
|
if( isLog( settings, level ) )
|
|
|
|
{
|
|
|
|
static const char *strLevel[] = { "NONE", "ERROR", "SQL", "INFO", "DATA" };
|
|
|
|
|
|
|
|
time_t t = ::time( 0 );
|
|
|
|
char *pString;
|
|
|
|
#ifdef SAL_W32
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf( settings->logFile, "%s [%s]: %s\n", pString, strLevel[level], str );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
0 , 0
|
|
|
|
},
|
|
|
|
{ 0, 0, 0, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
|
2012-10-01 21:51:40 +03:00
|
|
|
SAL_DLLPUBLIC_EXPORT void * SAL_CALL postgresql_sdbc_impl_component_getFactory(
|
2011-08-02 16:10:00 +02:00
|
|
|
const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
|
|
|
|
{
|
|
|
|
return cppu::component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-02-18 12:11:15 +00:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|