Files
libreoffice/dbaccess/source/core/api/table.cxx

822 lines
33 KiB
C++
Raw Normal View History

2000-09-18 23:16:46 +00:00
/*************************************************************************
*
* $RCSfile: table.cxx,v $
*
2002-03-27 13:59:31 +00:00
* $Revision: 1.43 $
2000-09-18 23:16:46 +00:00
*
2002-03-27 13:59:31 +00:00
* last change: $Author: oj $ $Date: 2002-03-27 14:59:31 $
2000-09-18 23:16:46 +00:00
*
* The Contents of this file are made available subject to the terms of
* either of the following licenses
*
* - GNU Lesser General Public License Version 2.1
* - Sun Industry Standards Source License Version 1.1
*
* Sun Microsystems Inc., October, 2000
*
* GNU Lesser General Public License Version 2.1
* =============================================
* Copyright 2000 by Sun Microsystems, Inc.
* 901 San Antonio Road, Palo Alto, CA 94303, USA
*
* 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.
*
* 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.
*
* 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
*
*
* Sun Industry Standards Source License Version 1.1
* =================================================
* The contents of this file are subject to the Sun Industry Standards
* Source License Version 1.1 (the "License"); You may not use this file
* except in compliance with the License. You may obtain a copy of the
* License at http://www.openoffice.org/license.html.
*
* Software provided under this License is provided on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
* See the License for the specific provisions governing your rights and
* obligations concerning the Software.
*
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
*
* Copyright: 2000 by Sun Microsystems, Inc.
*
* All Rights Reserved.
*
* Contributor(s): _______________________________________
*
*
************************************************************************/
#ifndef _DBA_CORE_TABLE_HXX_
#include "table.hxx"
#endif
#ifndef _DBACORE_DEFINITIONCOLUMN_HXX_
#include <definitioncolumn.hxx>
#endif
2000-10-25 06:32:52 +00:00
#ifndef DBACCESS_SHARED_DBASTRINGS_HRC
#include "dbastrings.hrc"
2000-09-18 23:16:46 +00:00
#endif
#ifndef _TOOLS_DEBUG_HXX
#include <tools/debug.hxx>
#endif
#ifndef _CPPUHELPER_TYPEPROVIDER_HXX_
#include <cppuhelper/typeprovider.hxx>
#endif
2000-10-11 10:21:40 +00:00
#ifndef _COMPHELPER_ENUMHELPER_HXX_
#include <comphelper/enumhelper.hxx>
2000-09-18 23:16:46 +00:00
#endif
2000-10-11 10:21:40 +00:00
#ifndef _COMPHELPER_CONTAINER_HXX_
#include <comphelper/container.hxx>
2000-09-18 23:16:46 +00:00
#endif
2000-10-11 10:21:40 +00:00
#ifndef _COMPHELPER_SEQUENCE_HXX_
#include <comphelper/sequence.hxx>
2000-09-18 23:16:46 +00:00
#endif
2000-10-25 06:32:52 +00:00
#ifndef _COMPHELPER_TYPES_HXX_
#include <comphelper/types.hxx>
#endif
2000-09-18 23:16:46 +00:00
#ifndef _COM_SUN_STAR_UTIL_XREFRESHLISTENER_HPP_
#include <com/sun/star/util/XRefreshListener.hpp>
#endif
#ifndef _COM_SUN_STAR_SDBC_XCONNECTION_HPP_
#include <com/sun/star/sdbc/XConnection.hpp>
#endif
#ifndef _COM_SUN_STAR_SDBC_XROW_HPP_
#include <com/sun/star/sdbc/XRow.hpp>
#endif
#ifndef _COM_SUN_STAR_SDBCX_PRIVILEGE_HPP_
#include <com/sun/star/sdbcx/Privilege.hpp>
#endif
#ifndef DBACCESS_CORE_API_KEYS_HXX
#include "CKeys.hxx"
#endif
#ifndef DBACCESS_INDEXES_HXX_
#include "CIndexes.hxx"
#endif
2000-11-08 13:23:39 +00:00
#ifndef _CONNECTIVITY_DBTOOLS_HXX_
#include <connectivity/dbtools.hxx>
#endif
2001-02-14 12:18:24 +00:00
#ifndef _DBHELPER_DBEXCEPTION_HXX_
#include <connectivity/dbexception.hxx>
#endif
#ifndef _COMPHELPER_EXTRACT_HXX_
#include <comphelper/extract.hxx>
2001-02-14 12:18:24 +00:00
#endif
2001-04-06 07:59:21 +00:00
#ifndef _CONNECTIVITY_SDBCX_COLUMN_HXX_
#include <connectivity/sdbcx/VColumn.hxx>
#endif
#ifndef DBACORE_SDBCORETOOLS_HXX
#include "sdbcoretools.hxx"
#endif
#ifndef _COM_SUN_STAR_SDBC_XRESULTSETMETADATA_HPP_
#include <com/sun/star/sdbc/XResultSetMetaData.hpp>
#endif
#ifndef _COM_SUN_STAR_SDBC_XRESULTSETMETADATASUPPLIER_HPP_
#include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
#endif
2000-09-18 23:16:46 +00:00
using namespace dbaccess;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::util;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::sdbc;
using namespace ::com::sun::star::sdbcx;
using namespace ::com::sun::star::container;
using namespace ::osl;
2000-10-11 10:21:40 +00:00
using namespace ::comphelper;
2000-09-18 23:16:46 +00:00
using namespace ::cppu;
using namespace ::utl;
2000-09-18 23:16:46 +00:00
typedef ::std::map <sal_Int32, OTableColumn*, std::less <sal_Int32> > OColMap;
//==========================================================================
//= ODBTable
//==========================================================================
DBG_NAME(ODBTable)
//--------------------------------------------------------------------------
ODBTable::ODBTable(connectivity::sdbcx::OCollection* _pTables,const OConfigurationNode& _rTableConfig,
const Reference< XConnection >& _rxConn,
2000-10-17 09:19:03 +00:00
const ::rtl::OUString& _rCatalog,
const ::rtl::OUString& _rSchema,
const ::rtl::OUString& _rName,
const ::rtl::OUString& _rType,
const ::rtl::OUString& _rDesc) throw(SQLException)
:connectivity::sdbcx::OTable(_pTables, _rxConn->getMetaData()->storesMixedCaseQuotedIdentifiers(), _rName, _rType, _rDesc, _rSchema, _rCatalog )
,OConfigurationFlushable(m_aMutex,_rTableConfig.isValid() ? _rTableConfig.cloneAsRoot() : OConfigurationTreeRoot())
,m_nPrivileges(0)
,m_xConnection( _rxConn )
,m_xMetaData( _rxConn->getMetaData() )
2000-09-18 23:16:46 +00:00
{
DBG_CTOR(ODBTable, NULL);
2000-10-17 09:19:03 +00:00
osl_incrementInterlockedCount( &m_refCount );
2001-10-26 06:50:50 +00:00
try
{
DBG_ASSERT(m_xMetaData.is(), "ODBTable::ODBTable : invalid conn !");
DBG_ASSERT(_rName.getLength(), "ODBTable::ODBTable : name !");
// register our properties
construct();
refreshColumns();
2000-12-12 11:20:31 +00:00
2001-10-26 06:50:50 +00:00
// load the settings from the configuration
if(m_aConfigurationNode.isValid())
// our own settings
loadFrom(m_aConfigurationNode.openNode(CONFIGKEY_SETTINGS));
2000-09-18 23:16:46 +00:00
2001-10-26 06:50:50 +00:00
// we don't collect the privileges here, this is potentially expensive. Instead we determine them on request.
// (see getFastPropertyValue)
m_nPrivileges = -1;
}
catch(Exception&)
{
}
2000-10-17 09:19:03 +00:00
osl_decrementInterlockedCount( &m_refCount );
2000-09-18 23:16:46 +00:00
// TODO : think about collecting the privileges here, as we can't ensure that in getFastPropertyValue, where
// we do this at the moment, the statement needed can be supplied by the connection (for example the SQL-Server
// ODBC driver does not allow more than one statement per connection, and in getFastPropertyValue it's more
// likely that it's already used up than it's here.)
}
2001-02-14 12:18:24 +00:00
// -----------------------------------------------------------------------------
ODBTable::ODBTable(connectivity::sdbcx::OCollection* _pTables, const Reference< XConnection >& _rxConn )
throw(SQLException)
:connectivity::sdbcx::OTable(_pTables, _rxConn->getMetaData()->storesMixedCaseQuotedIdentifiers())
,OConfigurationFlushable(m_aMutex)
,m_nPrivileges(-1)
,m_xConnection( _rxConn )
,m_xMetaData( _rxConn->getMetaData() )
2001-02-14 12:18:24 +00:00
{
construct();
}
2000-10-17 09:19:03 +00:00
// -------------------------------------------------------------------------
2000-09-18 23:16:46 +00:00
ODBTable::~ODBTable()
{
DBG_DTOR(ODBTable, NULL);
}
2001-04-06 07:59:21 +00:00
//--------------------------------------------------------------------------
OColumn* ODBTable::createColumn(const ::rtl::OUString& _rName) const
{
OColumn* pReturn = NULL;
Reference< XNamed > xRet = NULL;
if(m_xDriverColumns.is() && m_xDriverColumns->hasByName(_rName))
{
Reference<XPropertySet> xProp;
m_xDriverColumns->getByName(_rName) >>= xProp;
pReturn = new OTableColumnWrapper(xProp);
}
else
{
Any aCatalog;
aCatalog = const_cast<ODBTable*>(this)->getPropertyValue(PROPERTY_CATALOGNAME);
::rtl::OUString aSchema, aTable;
const_cast<ODBTable*>(this)->getPropertyValue(PROPERTY_SCHEMANAME) >>= aSchema;
const_cast<ODBTable*>(this)->getPropertyValue(PROPERTY_NAME) >>= aTable;
Reference< XResultSet > xResult = m_xMetaData->getColumns(aCatalog, aSchema, aTable, _rName);
if(xResult.is())
{
Reference< XRow > xRow(xResult,UNO_QUERY);
while(xResult->next())
{
if(xRow->getString(4) == _rName)
{
sal_Int32 nField5 = xRow->getInt(5);
::rtl::OUString aField6 = xRow->getString(6);
sal_Int32 nField7 = xRow->getInt(7)
, nField9 = xRow->getInt(9)
, nField11= xRow->getInt(11);
::rtl::OUString sField13 = xRow->getString(13);
::comphelper::disposeComponent(xRow);
// we need some more information about the column
static ::rtl::OUString STR_WHERE = ::rtl::OUString::createFromAscii(" WHERE ");
const ::rtl::OUString sQuote = m_xMetaData->getIdentifierQuoteString();
::rtl::OUString sSelect = ::rtl::OUString::createFromAscii("SELECT ");
sSelect += ::dbtools::quoteName(sQuote,_rName);
::rtl::OUString sComposedName;
::dbtools::composeTableName(m_xMetaData,getString(aCatalog),aSchema,aTable,sComposedName,sal_True);
sSelect += ::rtl::OUString::createFromAscii(" FROM ");
sSelect += sComposedName;
sSelect += STR_WHERE;
sSelect += ::rtl::OUString::createFromAscii(" 0 = 1");
sal_Bool bAutoIncrement = sal_False
,bIsCurrency = sal_False;
try
{
Reference<XStatement> xStmt = m_xConnection->createStatement();
xResult = xStmt->executeQuery(sSelect);
if(xResult.is())
{
Reference<XResultSetMetaData> xMD = Reference<XResultSetMetaDataSupplier>(xResult,UNO_QUERY)->getMetaData();
bAutoIncrement = xMD->isAutoIncrement(1);
bIsCurrency = xMD->isCurrency(1);
::comphelper::disposeComponent(xStmt);
}
}
catch(SQLException&)
{
}
2001-04-06 07:59:21 +00:00
connectivity::sdbcx::OColumn* pRet = new connectivity::sdbcx::OColumn(_rName,
aField6,
sField13,
2001-04-06 07:59:21 +00:00
nField11,
nField7,
nField9,
nField5,
bAutoIncrement,
sal_False,
bIsCurrency,
isCaseSensitive());
2001-04-06 07:59:21 +00:00
Reference<XPropertySet> xProp = pRet;
pReturn = new OTableColumnWrapper(xProp);
break;
}
}
}
}
return pReturn;
}
2000-09-18 23:16:46 +00:00
//--------------------------------------------------------------------------
Sequence< sal_Int8 > ODBTable::getImplementationId() throw (RuntimeException)
{
static OImplementationId * pId = 0;
if (! pId)
{
MutexGuard aGuard( Mutex::getGlobalMutex() );
if (! pId)
{
static OImplementationId aId;
pId = &aId;
}
}
return pId->getImplementationId();
}
// OComponentHelper
//------------------------------------------------------------------------------
2000-11-03 13:32:31 +00:00
void SAL_CALL ODBTable::disposing()
2000-09-18 23:16:46 +00:00
{
OTable_Base::disposing();
2000-12-12 11:20:31 +00:00
OConfigurationFlushable::disposing();
2000-09-18 23:16:46 +00:00
MutexGuard aGuard(m_aMutex);
m_xMetaData = NULL;
2000-09-18 23:16:46 +00:00
}
//------------------------------------------------------------------------------
void ODBTable::getFastPropertyValue(Any& _rValue, sal_Int32 _nHandle) const
{
if ((PROPERTY_ID_PRIVILEGES == _nHandle) && (-1 == m_nPrivileges))
{ // somebody is asking for the privileges an we do not know them, yet
const_cast<ODBTable*>(this)->m_nPrivileges = 0; // don't allow anything if something goes wrong
try
{
Any aVal;
if(m_CatalogName.getLength())
aVal <<= m_CatalogName;
Reference< XResultSet > xPrivileges = m_xMetaData->getTablePrivileges(aVal, m_SchemaName, m_Name);
2000-09-18 23:16:46 +00:00
Reference< XRow > xCurrentRow(xPrivileges, UNO_QUERY);
if (xCurrentRow.is())
{
::rtl::OUString sUserWorkingFor = m_xMetaData->getUserName();
static const ::rtl::OUString sSELECT = ::rtl::OUString::createFromAscii("SELECT");
static const ::rtl::OUString sINSERT = ::rtl::OUString::createFromAscii("INSERT");
static const ::rtl::OUString sUPDATE = ::rtl::OUString::createFromAscii("UPDATE");
static const ::rtl::OUString sDELETE = ::rtl::OUString::createFromAscii("DELETE");
static const ::rtl::OUString sREAD = ::rtl::OUString::createFromAscii("READ");
static const ::rtl::OUString sCREATE = ::rtl::OUString::createFromAscii("CREATE");
static const ::rtl::OUString sALTER = ::rtl::OUString::createFromAscii("ALTER");
static const ::rtl::OUString sREFERENCE = ::rtl::OUString::createFromAscii("REFERENCE");
static const ::rtl::OUString sDROP = ::rtl::OUString::createFromAscii("DROP");
2000-12-12 11:20:31 +00:00
// after creation the set is positioned before the first record, per definitionem
2000-09-18 23:16:46 +00:00
::rtl::OUString sPrivilege, sGrantee;
2000-12-06 08:57:54 +00:00
while (xPrivileges->next())
2000-09-18 23:16:46 +00:00
{
#ifdef DBG_UTIL
::rtl::OUString sCat, sSchema, sName, sGrantor, sGrantable;
2000-12-06 08:57:54 +00:00
sCat = xCurrentRow->getString(1);
sSchema = xCurrentRow->getString(2);
sName = xCurrentRow->getString(3);
sGrantor = xCurrentRow->getString(4);
2000-09-18 23:16:46 +00:00
#endif
2000-12-06 08:57:54 +00:00
sGrantee = xCurrentRow->getString(5);
sPrivilege = xCurrentRow->getString(6);
2000-09-18 23:16:46 +00:00
#ifdef DBG_UTIL
2000-12-06 08:57:54 +00:00
sGrantable = xCurrentRow->getString(7);
2000-09-18 23:16:46 +00:00
#endif
if (!sUserWorkingFor.equalsIgnoreAsciiCase(sGrantee))
2000-09-18 23:16:46 +00:00
continue;
if (sPrivilege.equalsIgnoreAsciiCase(sSELECT))
2000-09-18 23:16:46 +00:00
const_cast<ODBTable*>(this)->m_nPrivileges |= Privilege::SELECT;
else if (sPrivilege.equalsIgnoreAsciiCase(sINSERT))
2000-09-18 23:16:46 +00:00
const_cast<ODBTable*>(this)->m_nPrivileges |= Privilege::INSERT;
else if (sPrivilege.equalsIgnoreAsciiCase(sUPDATE))
2000-09-18 23:16:46 +00:00
const_cast<ODBTable*>(this)->m_nPrivileges |= Privilege::UPDATE;
else if (sPrivilege.equalsIgnoreAsciiCase(sDELETE))
2000-09-18 23:16:46 +00:00
const_cast<ODBTable*>(this)->m_nPrivileges |= Privilege::DELETE;
else if (sPrivilege.equalsIgnoreAsciiCase(sREAD))
2000-09-18 23:16:46 +00:00
const_cast<ODBTable*>(this)->m_nPrivileges |= Privilege::READ;
else if (sPrivilege.equalsIgnoreAsciiCase(sCREATE))
2000-09-18 23:16:46 +00:00
const_cast<ODBTable*>(this)->m_nPrivileges |= Privilege::CREATE;
else if (sPrivilege.equalsIgnoreAsciiCase(sALTER))
2000-09-18 23:16:46 +00:00
const_cast<ODBTable*>(this)->m_nPrivileges |= Privilege::ALTER;
else if (sPrivilege.equalsIgnoreAsciiCase(sREFERENCE))
2000-09-18 23:16:46 +00:00
const_cast<ODBTable*>(this)->m_nPrivileges |= Privilege::REFERENCE;
else if (sPrivilege.equalsIgnoreAsciiCase(sDROP))
2000-09-18 23:16:46 +00:00
const_cast<ODBTable*>(this)->m_nPrivileges |= Privilege::DROP;
}
}
disposeComponent(xPrivileges);
}
catch(SQLException& e)
{
static ::rtl::OUString sNotSupportedState = ::rtl::OUString::createFromAscii("IM001");
// some drivers don't support any privileges so we assume that we are allowed to do all we want :-)
if(e.SQLState == sNotSupportedState)
const_cast<ODBTable*>(this)->m_nPrivileges |= Privilege::DROP |
Privilege::REFERENCE |
Privilege::ALTER |
Privilege::CREATE |
Privilege::READ |
Privilege::DELETE |
Privilege::UPDATE |
Privilege::INSERT |
Privilege::SELECT;
else
DBG_ERROR("ODBTable::ODBTable : could not collect the privileges !");
2000-09-18 23:16:46 +00:00
}
}
OTable_Base::getFastPropertyValue(_rValue, _nHandle);
}
// -------------------------------------------------------------------------
void ODBTable::construct()
{
OTable_Base::construct();
registerProperty(PROPERTY_FILTER, PROPERTY_ID_FILTER, PropertyAttribute::BOUND,
&m_sFilter, ::getCppuType(&m_sFilter));
registerProperty(PROPERTY_ORDER, PROPERTY_ID_ORDER, PropertyAttribute::BOUND,
&m_sOrder, ::getCppuType(&m_sOrder));
registerProperty(PROPERTY_APPLYFILTER, PROPERTY_ID_APPLYFILTER, PropertyAttribute::BOUND,
&m_bApplyFilter, ::getBooleanCppuType());
registerProperty(PROPERTY_FONT, PROPERTY_ID_FONT, PropertyAttribute::BOUND,
&m_aFont, ::getCppuType(&m_aFont));
registerMayBeVoidProperty(PROPERTY_ROW_HEIGHT, PROPERTY_ID_ROW_HEIGHT, PropertyAttribute::BOUND | PropertyAttribute::MAYBEVOID,
&m_aRowHeight, ::getCppuType(static_cast<sal_Int32*>(NULL)));
registerMayBeVoidProperty(PROPERTY_TEXTCOLOR, PROPERTY_ID_TEXTCOLOR, PropertyAttribute::BOUND | PropertyAttribute::MAYBEVOID,
&m_aTextColor, ::getCppuType(static_cast<sal_Int32*>(NULL)));
2000-12-06 08:57:54 +00:00
registerProperty(PROPERTY_PRIVILEGES, PROPERTY_ID_PRIVILEGES, PropertyAttribute::BOUND | PropertyAttribute::READONLY,
2000-12-06 08:57:54 +00:00
&m_nPrivileges, ::getCppuType(static_cast<sal_Int32*>(NULL)));
registerMayBeVoidProperty(PROPERTY_TEXTLINECOLOR, PROPERTY_ID_TEXTLINECOLOR, PropertyAttribute::BOUND | PropertyAttribute::MAYBEVOID,
&m_aTextLineColor, ::getCppuType(static_cast<sal_Int32*>(NULL)));
registerProperty(PROPERTY_TEXTEMPHASIS, PROPERTY_ID_TEXTEMPHASIS, PropertyAttribute::BOUND,
&m_nFontEmphasis, ::getCppuType(&m_nFontEmphasis));
registerProperty(PROPERTY_TEXTRELIEF, PROPERTY_ID_TEXTRELIEF, PropertyAttribute::BOUND,
&m_nFontRelief, ::getCppuType(&m_nFontRelief));
2000-09-18 23:16:46 +00:00
}
2000-12-12 11:20:31 +00:00
// -----------------------------------------------------------------------------
::cppu::IPropertyArrayHelper* ODBTable::createArrayHelper( sal_Int32 _nId) const
{
Sequence< Property > aProps;
describeProperties(aProps);
if(!_nId)
{
Property* pBegin = aProps.getArray();
Property* pEnd = pBegin + aProps.getLength();
2000-12-12 11:20:31 +00:00
for(;pBegin != pEnd;++pBegin)
{
2001-06-12 12:40:09 +00:00
if (0 == pBegin->Name.compareToAscii(PROPERTY_CATALOGNAME))
2000-12-12 11:20:31 +00:00
pBegin->Attributes = PropertyAttribute::READONLY;
2001-06-12 12:40:09 +00:00
else if (0 == pBegin->Name.compareToAscii(PROPERTY_SCHEMANAME))
2000-12-12 11:20:31 +00:00
pBegin->Attributes = PropertyAttribute::READONLY;
2001-06-12 12:40:09 +00:00
else if (0 == pBegin->Name.compareToAscii(PROPERTY_DESCRIPTION))
2000-12-12 11:20:31 +00:00
pBegin->Attributes = PropertyAttribute::READONLY;
2001-06-12 12:40:09 +00:00
else if (0 == pBegin->Name.compareToAscii(PROPERTY_NAME))
2000-12-12 11:20:31 +00:00
pBegin->Attributes = PropertyAttribute::READONLY;
}
}
return new ::cppu::OPropertyArrayHelper(aProps);
}
// -----------------------------------------------------------------------------
::cppu::IPropertyArrayHelper & SAL_CALL ODBTable::getInfoHelper()
{
return *ODBTable_PROP::getArrayHelper(isNew() ? 1 : 0);
}
2000-09-18 23:16:46 +00:00
// -------------------------------------------------------------------------
// XServiceInfo
IMPLEMENT_SERVICE_INFO1(ODBTable, "com.sun.star.sdb.dbaccess.ODBTable", SERVICE_SDBCX_TABLE)
// -------------------------------------------------------------------------
Any SAL_CALL ODBTable::queryInterface( const Type & rType ) throw(RuntimeException)
{
Any aRet;
if(rType == getCppuType( (Reference<XRename>*)0))
return Any();
if(rType == getCppuType( (Reference<XAlterTable>*)0))
return Any();
aRet = OTable_Base::queryInterface( rType);
if(!aRet.hasValue())
aRet = OConfigurationFlushable::queryInterface( rType);
return aRet;
2000-09-18 23:16:46 +00:00
}
// -------------------------------------------------------------------------
Sequence< Type > SAL_CALL ODBTable::getTypes( ) throw(RuntimeException)
{
Type aRenameType = getCppuType( (Reference<XRename>*)0);
Type aAlterType = getCppuType( (Reference<XAlterTable>*)0);
2000-09-18 23:16:46 +00:00
Sequence< Type > aTypes(OTable_Base::getTypes());
2001-10-18 12:24:06 +00:00
::std::vector<Type> aOwnTypes;
aOwnTypes.reserve(aTypes.getLength());
2000-09-18 23:16:46 +00:00
const Type* pBegin = aTypes.getConstArray();
const Type* pEnd = pBegin + aTypes.getLength();
2001-10-18 12:24:06 +00:00
for(;pBegin != pEnd ;++pBegin)
{
if(*pBegin != aRenameType && *pBegin != aAlterType)
2001-10-18 12:24:06 +00:00
aOwnTypes.push_back(*pBegin);
2000-09-18 23:16:46 +00:00
}
2001-10-18 12:24:06 +00:00
Sequence< Type > aRet(aOwnTypes.begin(),aOwnTypes.size());
return ::comphelper::concatSequences(aRet,OConfigurationFlushable::getTypes());
2000-12-12 11:20:31 +00:00
}
// -----------------------------------------------------------------------------
void ODBTable::flush_NoBroadcast_NoCommit()
{
if(m_aConfigurationNode.isValid())
{
storeTo(m_aConfigurationNode.openNode(CONFIGKEY_SETTINGS));
2000-12-12 11:20:31 +00:00
OColumns* pColumns = static_cast<OColumns*>(m_pColumns);
if ( pColumns )
pColumns->storeSettings( m_aConfigurationNode.openNode(CONFIGKEY_QRYDESCR_COLUMNS), getDataSourceNumberFormats( m_xConnection ) );
2000-12-12 11:20:31 +00:00
}
2000-09-18 23:16:46 +00:00
}
// XRename,
//------------------------------------------------------------------------------
void SAL_CALL ODBTable::rename( const ::rtl::OUString& _rNewName ) throw(SQLException, ElementExistException, RuntimeException)
{
throw SQLException(::rtl::OUString::createFromAscii("Driver does not support this function!"),*this,::rtl::OUString::createFromAscii("IM001"),0,Any());
2000-09-18 23:16:46 +00:00
}
// XAlterTable,
//------------------------------------------------------------------------------
void SAL_CALL ODBTable::alterColumnByName( const ::rtl::OUString& _rName, const Reference< XPropertySet >& _rxDescriptor ) throw(SQLException, NoSuchElementException, RuntimeException)
{
if(m_pColumns->hasByName(_rName))
2001-02-14 12:18:24 +00:00
{
::rtl::OUString sSql = ::rtl::OUString::createFromAscii("ALTER TABLE ");
::rtl::OUString aQuote = m_xMetaData->getIdentifierQuoteString( );
::rtl::OUString sCatalog,sSchema,sTable,sComposedName;
if(m_xMetaData->supportsCatalogsInTableDefinitions())
sCatalog = m_CatalogName;
if(m_xMetaData->supportsSchemasInTableDefinitions())
sSchema = m_SchemaName;
::dbtools::composeTableName(m_xMetaData,sCatalog,sSchema,m_Name,sComposedName,sal_True);
if(!sComposedName.getLength())
::dbtools::throwFunctionSequenceException(*this);
2001-02-14 12:18:24 +00:00
sSql += sComposedName;
sSql += ::rtl::OUString::createFromAscii(" ALTER ");
sSql += ::dbtools::quoteName(aQuote,_rName);
::rtl::OUString sNewDefaultValue,sDefaultValue;
Reference<XPropertySet> xColumn;
m_pColumns->getByName(_rName) >>= xColumn;
if(_rxDescriptor->getPropertySetInfo()->hasPropertyByName(PROPERTY_DEFAULTVALUE))
_rxDescriptor->getPropertyValue(PROPERTY_DEFAULTVALUE) >>= sNewDefaultValue;
if(xColumn.is() && xColumn->getPropertySetInfo()->hasPropertyByName(PROPERTY_DEFAULTVALUE))
xColumn->getPropertyValue(PROPERTY_DEFAULTVALUE) >>= sDefaultValue;
if(sNewDefaultValue != sDefaultValue)
{
if(sNewDefaultValue.getLength())
{
sSql += ::rtl::OUString::createFromAscii(" SET DEFAULT ");
sSql += sNewDefaultValue;
}
else
sSql += ::rtl::OUString::createFromAscii(" DROP DEFAULT");
OSL_ENSURE(m_xMetaData->getConnection().is(),"Connection is null!");
Reference< XStatement > xStmt = m_xMetaData->getConnection()->createStatement( );
if(xStmt.is())
xStmt->execute(sSql);
}
else
// not supported
throw SQLException(::rtl::OUString::createFromAscii("Driver does not support this function!"),*this,::rtl::OUString::createFromAscii("IM001"),0,Any());
}
else
// not supported
throw SQLException(::rtl::OUString::createFromAscii("Driver does not support this function!"),*this,::rtl::OUString::createFromAscii("IM001"),0,Any());
m_pColumns->refresh();
2000-09-18 23:16:46 +00:00
}
//------------------------------------------------------------------------------
void SAL_CALL ODBTable::alterColumnByIndex( sal_Int32 _nIndex, const Reference< XPropertySet >& _rxDescriptor ) throw(SQLException, IndexOutOfBoundsException, RuntimeException)
{
if(_nIndex < m_pColumns->getCount())
2001-02-14 12:18:24 +00:00
{
Reference<XPropertySet> xColumn;
m_pColumns->getByIndex(_nIndex) >>= xColumn;
alterColumnByName(::comphelper::getString(xColumn->getPropertyValue(PROPERTY_NAME)),_rxDescriptor);
}
else // not supported
throw SQLException(::rtl::OUString::createFromAscii("Driver does not support this function!"),*this,::rtl::OUString::createFromAscii("IM001"),0,Any());
2000-09-18 23:16:46 +00:00
}
// -------------------------------------------------------------------------
void ODBTable::refreshColumns()
{
::std::vector< ::rtl::OUString> aVector;
2001-02-14 12:18:24 +00:00
Reference<XNameAccess> xNames;
if(!isNew())
2000-10-17 09:19:03 +00:00
{
Any aVal;
if(m_CatalogName.getLength())
aVal <<= m_CatalogName;
Reference< XResultSet > xResult = m_xMetaData->getColumns(aVal,
2000-10-17 09:19:03 +00:00
m_SchemaName,m_Name,::rtl::OUString::createFromAscii("%"));
if(xResult.is())
{
Reference< XRow > xRow(xResult,UNO_QUERY);
while(xResult->next())
aVector.push_back(xRow->getString(4));
}
}
if(!m_pColumns)
{
OColumns* pCol = new OColumns(*this, m_aMutex, xNames, isCaseSensitive(), aVector, this,this,
m_xMetaData->supportsAlterTableWithAddColumn(),
m_xMetaData->supportsAlterTableWithDropColumn());
pCol->setParent(this);
if(m_pColumns)
delete m_pColumns;
2000-09-18 23:16:46 +00:00
m_pColumns = pCol;
}
else
m_pColumns->reFill(aVector);
2000-09-18 23:16:46 +00:00
// our column's settings
if (m_aConfigurationNode.isValid())
{
OColumns* pColumns = static_cast<OColumns*>(m_pColumns);
pColumns->loadSettings( m_aConfigurationNode.openNode(CONFIGKEY_QRYDESCR_COLUMNS), getDataSourceNumberFormats( m_xConnection ) );
}
2000-09-18 23:16:46 +00:00
}
// -------------------------------------------------------------------------
void ODBTable::refreshPrimaryKeys(std::vector< ::rtl::OUString>& _rKeys)
{
Any aVal;
if(m_CatalogName.getLength())
aVal <<= m_CatalogName;
Reference< XResultSet > xResult = m_xMetaData->getPrimaryKeys(aVal,m_SchemaName,m_Name);
2002-03-27 13:17:20 +00:00
Reference< XRow > xRow(xResult,UNO_QUERY);
2000-09-18 23:16:46 +00:00
2002-03-27 13:17:20 +00:00
if ( xRow.is() )
2000-09-18 23:16:46 +00:00
{
2002-03-27 13:17:20 +00:00
if ( xResult->next() ) // there can be only one primary key
2000-09-18 23:16:46 +00:00
{
2002-03-27 13:17:20 +00:00
::rtl::OUString sPkName = xRow->getString(6);
_rKeys.push_back(sPkName);
2000-09-18 23:16:46 +00:00
}
}
}
// -------------------------------------------------------------------------
void ODBTable::refreshForgeinKeys(std::vector< ::rtl::OUString>& _rKeys)
{
Any aVal;
if(m_CatalogName.getLength())
aVal <<= m_CatalogName;
2001-02-28 09:25:05 +00:00
Reference< XResultSet > xResult = m_xMetaData->getImportedKeys(aVal,m_SchemaName,m_Name);
2002-03-27 13:17:20 +00:00
Reference< XRow > xRow(xResult,UNO_QUERY);
2000-09-18 23:16:46 +00:00
2002-03-27 13:17:20 +00:00
if ( xRow.is() )
2000-09-18 23:16:46 +00:00
{
2002-03-27 13:17:20 +00:00
while( xResult->next() )
{
sal_Int32 nKeySeq = xRow->getInt(9);
if ( nKeySeq == 1 )
{ // only append when the sequnce number is 1 to forbid serveral inserting the same key name
::rtl::OUString sFkName = xRow->getString(12);
if ( !xRow->wasNull() && sFkName.getLength() )
_rKeys.push_back(sFkName);
}
}
2000-09-18 23:16:46 +00:00
}
}
// -------------------------------------------------------------------------
void ODBTable::refreshKeys()
{
::std::vector< ::rtl::OUString> aVector;
2001-02-14 12:18:24 +00:00
Reference<XIndexAccess> xKeys;
if(!isNew())
2001-02-14 12:18:24 +00:00
{
try { refreshPrimaryKeys(aVector); }
catch(SQLException&)
{ // allowed to fail
OSL_TRACE("ODBTable::refreshKeys: caught an exception while refreshing the primary keys\n");
}
try { refreshForgeinKeys(aVector); }
catch(SQLException&)
{ // allowed to fail
OSL_TRACE("ODBTable::refreshKeys: caught an exception while refreshing the foreign keys\n");
}
2001-02-14 12:18:24 +00:00
}
if(!m_pKeys)
m_pKeys = new OKeys(this,m_aMutex,aVector,xKeys);
else
m_pKeys->reFill(aVector);
2000-09-18 23:16:46 +00:00
}
// -------------------------------------------------------------------------
void ODBTable::refreshIndexes()
{
2001-02-14 12:18:24 +00:00
if(!isNew())
2000-09-18 23:16:46 +00:00
{
2001-02-14 12:18:24 +00:00
::std::vector< ::rtl::OUString> aVector;
Reference<XNameAccess> xNames;
Any aVal;
if(m_CatalogName.getLength())
aVal <<= m_CatalogName;
2000-11-15 14:57:40 +00:00
try
{
// fill indexes
Reference< XResultSet > xResult = m_xMetaData->getIndexInfo(aVal,m_SchemaName,m_Name,sal_False,sal_False);
if(xResult.is())
2000-11-15 14:57:40 +00:00
{
Reference< XRow > xRow(xResult,UNO_QUERY);
::rtl::OUString aName,aDot = ::rtl::OUString::createFromAscii(".");
while(xResult->next())
{
aName = xRow->getString(5);
if(aName.getLength())
aName += aDot;
aName += xRow->getString(6);
2002-03-27 13:59:31 +00:00
if ( !xRow->wasNull() && aName.getLength() )
aVector.push_back(aName);
}
2000-11-15 14:57:40 +00:00
}
2000-09-18 23:16:46 +00:00
}
catch(SQLException&)
{
// it could happen that a driver doesn't support this and throws an exception
}
2000-09-18 23:16:46 +00:00
if(!m_pIndexes)
m_pIndexes = new OIndexes(this,m_aMutex,aVector,xNames);
else
m_pIndexes->reFill(aVector);
2001-02-14 12:18:24 +00:00
}
2000-09-18 23:16:46 +00:00
}
2000-11-08 13:23:39 +00:00
// -----------------------------------------------------------------------------
::rtl::OUString SAL_CALL ODBTable::getName() throw(RuntimeException)
2000-11-08 13:23:39 +00:00
{
::rtl::OUString aVal;
dbtools::composeTableName(m_xMetaData,m_CatalogName,m_SchemaName,m_Name,aVal,sal_False);
2000-11-08 13:23:39 +00:00
return aVal;
}
// -----------------------------------------------------------------------------
2001-05-04 09:02:31 +00:00
sal_Int64 SAL_CALL ODBTable::getSomething( const Sequence< sal_Int8 >& rId ) throw(RuntimeException)
2001-02-23 14:22:32 +00:00
{
sal_Int64 nRet(0);
2001-05-04 09:02:31 +00:00
if (rId.getLength() == 16 && 0 == rtl_compareMemory(getUnoTunnelImplementationId().getConstArray(), rId.getConstArray(), 16 ) )
nRet = (sal_Int64)this;
else
{
nRet = OTable_Base::getSomething(rId);
if(!nRet)
nRet = OConfigurationFlushable::getSomething(rId);
}
return nRet;
2001-05-04 09:02:31 +00:00
}
// -----------------------------------------------------------------------------
Sequence< sal_Int8 > ODBTable::getUnoTunnelImplementationId()
{
static ::cppu::OImplementationId * pId = 0;
if (! pId)
{
::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
if (! pId)
{
static ::cppu::OImplementationId aId;
pId = &aId;
}
}
return pId->getImplementationId();
}
// -----------------------------------------------------------------------------
Reference< XPropertySet > ODBTable::createEmptyObject()
{
2001-10-12 11:02:56 +00:00
return new OTableColumnDescriptor();
2001-02-23 14:22:32 +00:00
}
// -----------------------------------------------------------------------------
2001-04-30 09:16:19 +00:00
// -----------------------------------------------------------------------------
void SAL_CALL ODBTable::acquire() throw()
2001-04-30 09:16:19 +00:00
{
OTable_Base::acquire();
}
// -----------------------------------------------------------------------------
void SAL_CALL ODBTable::release() throw()
2001-04-30 09:16:19 +00:00
{
OTable_Base::release();
}
// -----------------------------------------------------------------------------
2001-02-23 14:22:32 +00:00
2000-09-18 23:16:46 +00:00