summaryrefslogtreecommitdiff
path: root/connectivity/source/commontools
diff options
context:
space:
mode:
authorOcke Janssen <oj@openoffice.org>2002-10-25 08:01:47 +0000
committerOcke Janssen <oj@openoffice.org>2002-10-25 08:01:47 +0000
commit5d916ad62fad3cf5af687432d0f40e4ccfdcd1e6 (patch)
tree0ac8da2679ee9efc3480de220f1ba7d56e6d7e9c /connectivity/source/commontools
parent01b1965f15d998dc40c93fc45c72e79ff3903ade (diff)
#104495# new base classes for SDBCX
Diffstat (limited to 'connectivity/source/commontools')
-rw-r--r--connectivity/source/commontools/TColumnsHelper.cxx200
-rw-r--r--connectivity/source/commontools/TIndex.cxx154
-rw-r--r--connectivity/source/commontools/TIndexColumns.cxx189
-rw-r--r--connectivity/source/commontools/TIndexes.cxx276
-rw-r--r--connectivity/source/commontools/TKey.cxx164
-rw-r--r--connectivity/source/commontools/TKeyColumns.cxx207
-rw-r--r--connectivity/source/commontools/TKeys.cxx357
-rw-r--r--connectivity/source/commontools/TTableHelper.cxx368
-rw-r--r--connectivity/source/commontools/dbtools2.cxx603
9 files changed, 2518 insertions, 0 deletions
diff --git a/connectivity/source/commontools/TColumnsHelper.cxx b/connectivity/source/commontools/TColumnsHelper.cxx
new file mode 100644
index 000000000000..c372de9e20d9
--- /dev/null
+++ b/connectivity/source/commontools/TColumnsHelper.cxx
@@ -0,0 +1,200 @@
+/*************************************************************************
+ *
+ * $RCSfile: TColumnsHelper.cxx,v $
+ *
+ * $Revision: 1.1 $
+ *
+ * last change: $Author: oj $ $Date: 2002-10-25 09:01:08 $
+ *
+ * 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 CONNECTIVITY_COLUMNSHELPER_HXX
+#include "connectivity/TColumnsHelper.hxx"
+#endif
+#ifndef _CONNECTIVITY_SDBCX_COLUMN_HXX_
+#include "connectivity/sdbcx/VColumn.hxx"
+#endif
+#ifndef _CONNECTIVITY_SDBCX_COLUMN_HXX_
+#include "connectivity/sdbcx/VColumn.hxx"
+#endif
+#ifndef _COM_SUN_STAR_SDBC_XROW_HPP_
+#include <com/sun/star/sdbc/XRow.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SDBC_XRESULTSET_HPP_
+#include <com/sun/star/sdbc/XResultSet.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SDBC_DATATYPE_HPP_
+#include <com/sun/star/sdbc/DataType.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SDBC_COLUMNVALUE_HPP_
+#include <com/sun/star/sdbc/ColumnValue.hpp>
+#endif
+#ifndef _COMPHELPER_TYPES_HXX_
+#include <comphelper/types.hxx>
+#endif
+#ifndef _CONNECTIVITY_DBTOOLS_HXX_
+#include "connectivity/dbtools.hxx"
+#endif
+#ifndef CONNECTIVITY_CONNECTION_HXX
+#include "TConnection.hxx"
+#endif
+#ifndef CONNECTIVITY_TABLEHELPER_HXX
+#include "connectivity/TTableHelper.hxx"
+#endif
+#ifndef _COMPHELPER_PROPERTY_HXX_
+#include <comphelper/property.hxx>
+#endif
+
+using namespace ::comphelper;
+
+
+using namespace connectivity::sdbcx;
+using namespace connectivity;
+using namespace ::com::sun::star::uno;
+using namespace ::com::sun::star::beans;
+// using namespace ::com::sun::star::sdbcx;
+using namespace ::com::sun::star::sdbc;
+using namespace ::com::sun::star::container;
+using namespace ::com::sun::star::lang;
+typedef connectivity::sdbcx::OCollection OCollection_TYPE;
+
+OColumnsHelper::OColumnsHelper( ::cppu::OWeakObject& _rParent,
+ sal_Bool _bCase,
+ ::osl::Mutex& _rMutex,
+ const TStringVector &_rVector
+ ) : OCollection(_rParent,_bCase,_rMutex,_rVector)
+ ,m_pTable(NULL)
+{
+}
+// -----------------------------------------------------------------------------
+
+Reference< XNamed > OColumnsHelper::createObject(const ::rtl::OUString& _rName)
+{
+ Reference< XNamed > xRet(::dbtools::createSDBCXColumn(m_pTable,m_pTable->getConnection(),_rName,isCaseSensitive()),UNO_QUERY);
+ return xRet;
+}
+
+// -------------------------------------------------------------------------
+void OColumnsHelper::impl_refresh() throw(RuntimeException)
+{
+ if ( m_pTable )
+ m_pTable->refreshColumns();
+}
+// -------------------------------------------------------------------------
+Reference< XPropertySet > OColumnsHelper::createEmptyObject()
+{
+ return new OColumn(sal_True);
+}
+// -----------------------------------------------------------------------------
+Reference< XNamed > OColumnsHelper::cloneObject(const Reference< XPropertySet >& _xDescriptor)
+{
+ Reference<XPropertySet> xProp = createEmptyObject();
+ ::comphelper::copyProperties(_xDescriptor,xProp);
+ Reference< XNamed > xName(xProp,UNO_QUERY);
+ OSL_ENSURE(xName.is(),"Must be a XName interface here !");
+ return xName;
+}
+// -----------------------------------------------------------------------------
+// XAppend
+void OColumnsHelper::appendObject( const Reference< XPropertySet >& descriptor )
+{
+ ::osl::MutexGuard aGuard(m_rMutex);
+ OSL_ENSURE(m_pTable,"OColumnsHelper::appendByDescriptor: Table is null!");
+ OSL_ENSURE(descriptor.is(),"OColumnsHelper::appendByDescriptor: descriptor is null!");
+
+ if ( descriptor.is() && m_pTable && !m_pTable->isNew() )
+ {
+ Reference<XDatabaseMetaData> xMetaData = m_pTable->getConnection()->getMetaData();
+ ::rtl::OUString aSql = ::rtl::OUString::createFromAscii("ALTER TABLE ");
+ ::rtl::OUString aQuote = xMetaData->getIdentifierQuoteString( );
+
+ aSql += ::dbtools::composeTableName(xMetaData,m_pTable,sal_True,::dbtools::eInTableDefinitions);
+ aSql += ::rtl::OUString::createFromAscii(" ADD ");
+ aSql += ::dbtools::createStandardColumnPart(descriptor,m_pTable->getConnection());
+
+ Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement( );
+ if ( xStmt.is() )
+ {
+ xStmt->execute(aSql);
+ ::comphelper::disposeComponent(xStmt);
+ }
+ }
+}
+// -------------------------------------------------------------------------
+// XDrop
+void OColumnsHelper::dropObject(sal_Int32 _nPos,const ::rtl::OUString _sElementName)
+{
+ OSL_ENSURE(m_pTable,"OColumnsHelper::dropByName: Table is null!");
+ if ( m_pTable && !m_pTable->isNew() )
+ {
+ ::rtl::OUString aSql = ::rtl::OUString::createFromAscii("ALTER TABLE ");
+ Reference<XDatabaseMetaData> xMetaData = m_pTable->getConnection()->getMetaData();
+ ::rtl::OUString aQuote = xMetaData->getIdentifierQuoteString( );
+
+ aSql += ::dbtools::composeTableName(xMetaData,m_pTable,sal_True,::dbtools::eInTableDefinitions);
+ aSql += ::rtl::OUString::createFromAscii(" DROP ");
+ aSql += ::dbtools::quoteName( aQuote,_sElementName);
+
+ Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement( );
+ if ( xStmt.is() )
+ {
+ xStmt->execute(aSql);
+ ::comphelper::disposeComponent(xStmt);
+ }
+ }
+}
+// -----------------------------------------------------------------------------
+
+
+
diff --git a/connectivity/source/commontools/TIndex.cxx b/connectivity/source/commontools/TIndex.cxx
new file mode 100644
index 000000000000..db9dd5230153
--- /dev/null
+++ b/connectivity/source/commontools/TIndex.cxx
@@ -0,0 +1,154 @@
+/*************************************************************************
+ *
+ * $RCSfile: TIndex.cxx,v $
+ *
+ * $Revision: 1.1 $
+ *
+ * last change: $Author: oj $ $Date: 2002-10-25 09:00:12 $
+ *
+ * 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 CONNECTIVITY_INDEXHELPER_HXX_
+#include "connectivity/TIndex.hxx"
+#endif
+#ifndef CONNECTIVITY_INDEXCOLUMNSHELPER_HXX
+#include "connectivity/TIndexColumns.hxx"
+#endif
+#ifndef _COM_SUN_STAR_SDBC_XROW_HPP_
+#include <com/sun/star/sdbc/XRow.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SDBC_XRESULTSET_HPP_
+#include <com/sun/star/sdbc/XResultSet.hpp>
+#endif
+#ifndef CONNECTIVITY_TABLEHELPER_HXX
+#include "connectivity/TTableHelper.hxx"
+#endif
+#ifndef CONNECTIVITY_CONNECTION_HXX
+#include "TConnection.hxx"
+#endif
+
+using namespace connectivity;
+using namespace connectivity::sdbcx;
+using namespace ::com::sun::star::uno;
+using namespace ::com::sun::star::beans;
+// using namespace ::com::sun::star::sdbcx;
+using namespace ::com::sun::star::sdbc;
+using namespace ::com::sun::star::container;
+using namespace ::com::sun::star::lang;
+// -------------------------------------------------------------------------
+OIndexHelper::OIndexHelper( OTableHelper* _pTable) : connectivity::sdbcx::OIndex(sal_True)
+ , m_pTable(_pTable)
+{
+ construct();
+ ::std::vector< ::rtl::OUString> aVector;
+ m_pColumns = new OIndexColumns(this,m_aMutex,aVector);
+}
+// -------------------------------------------------------------------------
+OIndexHelper::OIndexHelper( OTableHelper* _pTable,
+ const ::rtl::OUString& _Name,
+ const ::rtl::OUString& _Catalog,
+ sal_Bool _isUnique,
+ sal_Bool _isPrimaryKeyIndex,
+ sal_Bool _isClustered
+ ) : connectivity::sdbcx::OIndex(_Name,
+ _Catalog,
+ _isUnique,
+ _isPrimaryKeyIndex,
+ _isClustered,sal_True)
+ ,m_pTable(_pTable)
+{
+ construct();
+ refreshColumns();
+}
+// -------------------------------------------------------------------------
+
+void OIndexHelper::refreshColumns()
+{
+ if ( !m_pTable )
+ return;
+
+ ::std::vector< ::rtl::OUString> aVector;
+ if ( !isNew() )
+ {
+ ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
+ ::rtl::OUString aSchema,aTable;
+ m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_SCHEMANAME)) >>= aSchema;
+ m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)) >>= aTable;
+
+ Reference< XResultSet > xResult = m_pTable->getMetaData()->getIndexInfo(
+ m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME)),
+ aSchema,aTable,sal_False,sal_False);
+
+ if ( xResult.is() )
+ {
+ Reference< XRow > xRow(xResult,UNO_QUERY);
+ ::rtl::OUString aColName;
+ while( xResult->next() )
+ {
+ if ( xRow->getString(6) == m_Name )
+ {
+ aColName = xRow->getString(9);
+ if ( !xRow->wasNull() )
+ aVector.push_back(aColName);
+ }
+ }
+ }
+ }
+ if(m_pColumns)
+ m_pColumns->reFill(aVector);
+ else
+ m_pColumns = new OIndexColumns(this,m_aMutex,aVector);
+}
+// -----------------------------------------------------------------------------
+
diff --git a/connectivity/source/commontools/TIndexColumns.cxx b/connectivity/source/commontools/TIndexColumns.cxx
new file mode 100644
index 000000000000..406891f12876
--- /dev/null
+++ b/connectivity/source/commontools/TIndexColumns.cxx
@@ -0,0 +1,189 @@
+/*************************************************************************
+ *
+ * $RCSfile: TIndexColumns.cxx,v $
+ *
+ * $Revision: 1.1 $
+ *
+ * last change: $Author: oj $ $Date: 2002-10-25 09:01:47 $
+ *
+ * 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 CONNECTIVITY_INDEXCOLUMNSHELPER_HXX
+#include "connectivity/TIndexColumns.hxx"
+#endif
+#ifndef _CONNECTIVITY_SDBCX_INDEXCOLUMN_HXX_
+#include "connectivity/sdbcx/VIndexColumn.hxx"
+#endif
+#ifndef _COM_SUN_STAR_SDBC_XROW_HPP_
+#include <com/sun/star/sdbc/XRow.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SDBC_XRESULTSET_HPP_
+#include <com/sun/star/sdbc/XResultSet.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SDBC_DATATYPE_HPP_
+#include <com/sun/star/sdbc/DataType.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SDBC_COLUMNVALUE_HPP_
+#include <com/sun/star/sdbc/ColumnValue.hpp>
+#endif
+#ifndef _COMPHELPER_PROPERTY_HXX_
+#include <comphelper/property.hxx>
+#endif
+#ifndef CONNECTIVITY_INDEXHELPER_HXX_
+#include "connectivity/TIndex.hxx"
+#endif
+#ifndef CONNECTIVITY_TABLEHELPER_HXX
+#include "connectivity/TTableHelper.hxx"
+#endif
+#ifndef CONNECTIVITY_CONNECTION_HXX
+#include "TConnection.hxx"
+#endif
+
+using namespace connectivity;
+using namespace connectivity::sdbcx;
+using namespace ::com::sun::star::uno;
+using namespace ::com::sun::star::beans;
+// using namespace ::com::sun::star::sdbcx;
+using namespace ::com::sun::star::sdbc;
+using namespace ::com::sun::star::container;
+using namespace ::com::sun::star::lang;
+// -------------------------------------------------------------------------
+OIndexColumns::OIndexColumns( OIndexHelper* _pIndex,
+ ::osl::Mutex& _rMutex,
+ const ::std::vector< ::rtl::OUString> &_rVector)
+ : connectivity::sdbcx::OCollection(*_pIndex,sal_True,_rMutex,_rVector)
+ ,m_pIndex(_pIndex)
+{
+}
+// -------------------------------------------------------------------------
+Reference< XNamed > OIndexColumns::createObject(const ::rtl::OUString& _rName)
+{
+ ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
+ ::rtl::OUString aSchema,aTable;
+ m_pIndex->getTable()->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_SCHEMANAME)) >>= aSchema;
+ m_pIndex->getTable()->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)) >>= aTable;
+
+ Reference< XResultSet > xResult = m_pIndex->getTable()->getConnection()->getMetaData()->getIndexInfo(
+ m_pIndex->getTable()->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME)),
+ aSchema,aTable,sal_False,sal_False);
+
+ sal_Bool bAsc = sal_True;
+ if ( xResult.is() )
+ {
+ Reference< XRow > xRow(xResult,UNO_QUERY);
+ ::rtl::OUString aD(::rtl::OUString::createFromAscii("D"));
+ while( xResult->next() )
+ {
+ if(xRow->getString(9) == _rName)
+ bAsc = xRow->getString(10) != aD;
+ }
+ }
+
+ xResult = m_pIndex->getTable()->getConnection()->getMetaData()->getColumns(
+ m_pIndex->getTable()->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME)),
+ aSchema,aTable,_rName);
+
+ Reference< XNamed > xRet = NULL;
+ if ( xResult.is() )
+ {
+ Reference< XRow > xRow(xResult,UNO_QUERY);
+ while( xResult->next() )
+ {
+ if ( xRow->getString(4) == _rName )
+ {
+ sal_Int32 nDataType = xRow->getInt(5);
+ ::rtl::OUString aTypeName(xRow->getString(6));
+ sal_Int32 nSize = xRow->getInt(7);
+ sal_Int32 nDec = xRow->getInt(9);
+ sal_Int32 nNull = xRow->getInt(11);
+ ::rtl::OUString aColumnDef(xRow->getString(13));
+
+ OIndexColumn* pRet = new OIndexColumn(bAsc,
+ _rName,
+ aTypeName,
+ aColumnDef,
+ nNull,
+ nSize,
+ nDec,
+ nDataType,
+ sal_False,sal_False,sal_False,sal_True);
+ xRet = pRet;
+ break;
+ }
+ }
+ }
+
+ return xRet;
+}
+// -------------------------------------------------------------------------
+Reference< XPropertySet > OIndexColumns::createEmptyObject()
+{
+ return new OIndexColumn(sal_True);
+}
+// -------------------------------------------------------------------------
+void OIndexColumns::impl_refresh() throw(RuntimeException)
+{
+ m_pIndex->refreshColumns();
+}
+// -----------------------------------------------------------------------------
+Reference< XNamed > OIndexColumns::cloneObject(const Reference< XPropertySet >& _xDescriptor)
+{
+ OIndexColumn* pColumn = new OIndexColumn(sal_True);
+ Reference<XPropertySet> xProp = pColumn;
+ ::comphelper::copyProperties(_xDescriptor,xProp);
+ Reference< XNamed > xName(xProp,UNO_QUERY);
+ OSL_ENSURE(xName.is(),"Must be a XName interface here !");
+ return xName;
+}
+// -----------------------------------------------------------------------------
diff --git a/connectivity/source/commontools/TIndexes.cxx b/connectivity/source/commontools/TIndexes.cxx
new file mode 100644
index 000000000000..74423d255c03
--- /dev/null
+++ b/connectivity/source/commontools/TIndexes.cxx
@@ -0,0 +1,276 @@
+/*************************************************************************
+ *
+ * $RCSfile: TIndexes.cxx,v $
+ *
+ * $Revision: 1.1 $
+ *
+ * last change: $Author: oj $ $Date: 2002-10-25 09:01:34 $
+ *
+ * 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 CONNECTIVITY_INDEXESHELPER_HXX
+#include "connectivity/TIndexes.hxx"
+#endif
+#ifndef CONNECTIVITY_INDEXHELPER_HXX_
+#include "connectivity/TIndex.hxx"
+#endif
+#ifndef _COM_SUN_STAR_SDBC_XROW_HPP_
+#include <com/sun/star/sdbc/XRow.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SDBC_XRESULTSET_HPP_
+#include <com/sun/star/sdbc/XResultSet.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SDBC_INDEXTYPE_HPP_
+#include <com/sun/star/sdbc/IndexType.hpp>
+#endif
+#ifndef _CONNECTIVITY_DBTOOLS_HXX_
+#include <connectivity/dbtools.hxx>
+#endif
+#ifndef CONNECTIVITY_TABLEHELPER_HXX
+#include "connectivity/TTableHelper.hxx"
+#endif
+#ifndef CONNECTIVITY_CONNECTION_HXX
+#include "TConnection.hxx"
+#endif
+#ifndef _COMPHELPER_EXTRACT_HXX_
+#include <comphelper/extract.hxx>
+#endif
+
+using namespace connectivity;
+using namespace connectivity::sdbcx;
+using namespace ::com::sun::star::uno;
+using namespace ::com::sun::star::beans;
+using namespace ::com::sun::star::sdbcx;
+using namespace ::com::sun::star::sdbc;
+using namespace ::com::sun::star::container;
+using namespace ::com::sun::star::lang;
+using namespace cppu;
+
+typedef connectivity::sdbcx::OCollection OCollection_TYPE;
+// -----------------------------------------------------------------------------
+OIndexesHelper::OIndexesHelper(OTableHelper* _pTable,
+ ::osl::Mutex& _rMutex,
+ const ::std::vector< ::rtl::OUString> &_rVector
+ )
+ : OCollection(*_pTable,sal_True,_rMutex,_rVector)
+ ,m_pTable(_pTable)
+{
+}
+// -----------------------------------------------------------------------------
+
+Reference< XNamed > OIndexesHelper::createObject(const ::rtl::OUString& _rName)
+{
+ Reference< XNamed > xRet = NULL;
+ ::rtl::OUString aName,aQualifier;
+ sal_Int32 nLen = _rName.indexOf('.');
+ if ( nLen != -1 )
+ {
+ aQualifier = _rName.copy(0,nLen);
+ aName = _rName.copy(nLen+1);
+ }
+ else
+ aName = _rName;
+
+ ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
+ ::rtl::OUString aSchema,aTable;
+ m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_SCHEMANAME)) >>= aSchema;
+ m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)) >>= aTable;
+
+
+ Reference< XResultSet > xResult = m_pTable->getMetaData()->getIndexInfo(m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME)),aSchema,aTable,sal_False,sal_False);
+
+ if ( xResult.is() )
+ {
+ Reference< XRow > xRow(xResult,UNO_QUERY);
+ while( xResult->next() )
+ {
+ sal_Bool bUnique = !xRow->getBoolean(4);
+ if((!aQualifier.getLength() || xRow->getString(5) == aQualifier ) && xRow->getString(6) == aName)
+ {
+ OIndexHelper* pRet = new OIndexHelper(m_pTable,aName,aQualifier,bUnique,
+ sal_False,
+ xRow->getShort(7) == IndexType::CLUSTERED);
+ xRet = pRet;
+ break;
+ }
+ }
+ }
+
+ return xRet;
+}
+// -------------------------------------------------------------------------
+void OIndexesHelper::impl_refresh() throw(RuntimeException)
+{
+ m_pTable->refreshIndexes();
+}
+// -------------------------------------------------------------------------
+Reference< XPropertySet > OIndexesHelper::createEmptyObject()
+{
+ return new OIndexHelper(m_pTable);
+}
+// -------------------------------------------------------------------------
+// XAppend
+void OIndexesHelper::appendObject( const Reference< XPropertySet >& descriptor )
+{
+ ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
+ ::rtl::OUString aName = comphelper::getString(descriptor->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)));
+ ObjectMap::iterator aIter = m_aNameMap.find(aName);
+ if( aIter != m_aNameMap.end())
+ throw ElementExistException(aName,*this);
+
+ if(!m_pTable->isNew())
+ {
+ ::rtl::OUString aSql = ::rtl::OUString::createFromAscii("CREATE ");
+ ::rtl::OUString aQuote = m_pTable->getMetaData()->getIdentifierQuoteString( );
+ ::rtl::OUString aDot = ::rtl::OUString::createFromAscii(".");
+
+ if(comphelper::getINT32(descriptor->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_ISUNIQUE))))
+ aSql = aSql + ::rtl::OUString::createFromAscii("UNIQUE ");
+ aSql = aSql + ::rtl::OUString::createFromAscii("INDEX ");
+
+
+ ::rtl::OUString aCatalog,aSchema,aTable;
+ dbtools::qualifiedNameComponents(m_pTable->getMetaData(),m_pTable->getName(),aCatalog,aSchema,aTable,::dbtools::eInDataManipulation);
+ ::rtl::OUString aComposedName;
+
+ dbtools::composeTableName(m_pTable->getMetaData(),aCatalog,aSchema,aTable,aComposedName,sal_True,::dbtools::eInIndexDefinitions);
+ if ( aName.getLength() )
+ {
+ aSql = aSql + ::dbtools::quoteName( aQuote,aName )
+ + ::rtl::OUString::createFromAscii(" ON ")
+ + aComposedName
+ + ::rtl::OUString::createFromAscii(" ( ");
+
+ Reference<XColumnsSupplier> xColumnSup(descriptor,UNO_QUERY);
+ Reference<XIndexAccess> xColumns(xColumnSup->getColumns(),UNO_QUERY);
+ Reference< XPropertySet > xColProp;
+ for(sal_Int32 i=0;i<xColumns->getCount();++i)
+ {
+ ::cppu::extractInterface(xColProp,xColumns->getByIndex(i));
+ aSql = aSql + ::dbtools::quoteName( aQuote,comphelper::getString(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME))));
+ aSql = aSql + (any2bool(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_ISASCENDING)))
+ ?
+ ::rtl::OUString::createFromAscii(" ASC")
+ :
+ ::rtl::OUString::createFromAscii(" DESC"))
+ + ::rtl::OUString::createFromAscii(",");
+ }
+ aSql = aSql.replaceAt(aSql.getLength()-1,1,::rtl::OUString::createFromAscii(")"));
+ }
+ else
+ {
+ aSql = aSql + aComposedName;
+
+ Reference<XColumnsSupplier> xColumnSup(descriptor,UNO_QUERY);
+ Reference<XIndexAccess> xColumns(xColumnSup->getColumns(),UNO_QUERY);
+ Reference< XPropertySet > xColProp;
+ if(xColumns->getCount() != 1)
+ throw SQLException();
+
+ xColumns->getByIndex(0) >>= xColProp;
+
+ aSql = aSql + aDot + ::dbtools::quoteName( aQuote,comphelper::getString(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME))));
+ }
+
+ Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement( );
+ if ( xStmt.is() )
+ {
+ xStmt->execute(aSql);
+ ::comphelper::disposeComponent(xStmt);
+ }
+ }
+}
+// -------------------------------------------------------------------------
+// XDrop
+void OIndexesHelper::dropObject(sal_Int32 _nPos,const ::rtl::OUString _sElementName)
+{
+ ObjectMap::iterator aIter = m_aNameMap.find(_sElementName);
+
+ if(!m_pTable->isNew())
+ {
+ ::rtl::OUString aName,aSchema;
+ sal_Int32 nLen = _sElementName.indexOf('.');
+ if(nLen != -1)
+ aSchema = _sElementName.copy(0,nLen);
+ aName = _sElementName.copy(nLen+1);
+
+ ::rtl::OUString aSql = ::rtl::OUString::createFromAscii("DROP INDEX ");
+ ::rtl::OUString aQuote = m_pTable->getMetaData()->getIdentifierQuoteString( );
+ ::rtl::OUString aDot = ::rtl::OUString::createFromAscii(".");
+
+ ::rtl::OUString aComposedName = dbtools::composeTableName(m_pTable->getMetaData(),m_pTable,sal_True,::dbtools::eInIndexDefinitions);
+
+ aSql = aSql + ::dbtools::quoteName( aQuote,aSchema)
+ + aDot + ::dbtools::quoteName( aQuote,aName)
+ + ::rtl::OUString::createFromAscii(" ON ")
+ + aComposedName;
+
+ Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement( );
+ if ( xStmt.is() )
+ {
+ xStmt->execute(aSql);
+ ::comphelper::disposeComponent(xStmt);
+ }
+ }
+}
+// -----------------------------------------------------------------------------
+Reference< XNamed > OIndexesHelper::cloneObject(const Reference< XPropertySet >& _xDescriptor)
+{
+ Reference< XNamed > xName(_xDescriptor,UNO_QUERY);
+ OSL_ENSURE(xName.is(),"Must be a XName interface here !");
+ return xName.is() ? createObject(xName->getName()) : Reference< XNamed >();
+}
+// -----------------------------------------------------------------------------
+
+
+
diff --git a/connectivity/source/commontools/TKey.cxx b/connectivity/source/commontools/TKey.cxx
new file mode 100644
index 000000000000..e77e47ad3261
--- /dev/null
+++ b/connectivity/source/commontools/TKey.cxx
@@ -0,0 +1,164 @@
+/*************************************************************************
+ *
+ * $RCSfile: TKey.cxx,v $
+ *
+ * $Revision: 1.1 $
+ *
+ * last change: $Author: oj $ $Date: 2002-10-25 09:00:44 $
+ *
+ * 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 CONNECTIVITY_TKEY_HXX
+#include "connectivity/TKey.hxx"
+#endif
+#ifndef CONNECTIVITY_TKEYCOLUMNS_HXX
+#include "connectivity/TKeyColumns.hxx"
+#endif
+#ifndef _COM_SUN_STAR_SDBC_XROW_HPP_
+#include <com/sun/star/sdbc/XRow.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SDBC_XRESULTSET_HPP_
+#include <com/sun/star/sdbc/XResultSet.hpp>
+#endif
+#ifndef CONNECTIVITY_CONNECTION_HXX
+#include "TConnection.hxx"
+#endif
+#ifndef CONNECTIVITY_TABLEHELPER_HXX
+#include "connectivity/TTableHelper.hxx"
+#endif
+
+using namespace connectivity;
+using namespace ::com::sun::star::uno;
+using namespace ::com::sun::star::beans;
+// using namespace ::com::sun::star::sdbcx;
+using namespace ::com::sun::star::sdbc;
+using namespace ::com::sun::star::container;
+using namespace ::com::sun::star::lang;
+// -------------------------------------------------------------------------
+OTableKeyHelper::OTableKeyHelper(OTableHelper* _pTable) : connectivity::sdbcx::OKey(sal_True)
+ ,m_pTable(_pTable)
+{
+ construct();
+}
+// -------------------------------------------------------------------------
+OTableKeyHelper::OTableKeyHelper( OTableHelper* _pTable,
+ const ::rtl::OUString& _Name,
+ const ::rtl::OUString& _ReferencedTable,
+ sal_Int32 _Type,
+ sal_Int32 _UpdateRule,
+ sal_Int32 _DeleteRule
+ ) : connectivity::sdbcx::OKey(_Name,
+ _ReferencedTable,
+ _Type,
+ _UpdateRule,
+ _DeleteRule,sal_True)
+ ,m_pTable(_pTable)
+{
+ construct();
+ refreshColumns();
+}
+// -------------------------------------------------------------------------
+void OTableKeyHelper::refreshColumns()
+{
+ if ( !m_pTable )
+ return;
+
+ ::std::vector< ::rtl::OUString> aVector;
+ if ( !isNew() )
+ {
+ ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
+ ::rtl::OUString aSchema,aTable;
+ m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_SCHEMANAME)) >>= aSchema;
+ m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)) >>= aTable;
+
+ if ( m_Name.getLength() ) // foreign key
+ {
+
+ Reference< XResultSet > xResult = m_pTable->getMetaData()->getImportedKeys(m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME)),
+ aSchema,aTable);
+
+ if ( xResult.is() )
+ {
+ Reference< XRow > xRow(xResult,UNO_QUERY);
+ while( xResult->next() )
+ {
+ ::rtl::OUString aForeignKeyColumn = xRow->getString(8);
+ if(xRow->getString(12) == m_Name)
+ aVector.push_back(aForeignKeyColumn);
+ }
+ }
+ }
+ if ( aVector.empty() )
+ {
+ Reference< XResultSet > xResult = m_pTable->getMetaData()->getPrimaryKeys(m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME)),
+ aSchema,aTable);
+
+ if ( xResult.is() )
+ {
+ Reference< XRow > xRow(xResult,UNO_QUERY);
+ while( xResult->next() )
+ aVector.push_back(xRow->getString(4));
+ }
+ }
+ }
+
+
+ if ( m_pColumns )
+ m_pColumns ->reFill(aVector);
+ else
+ m_pColumns = new OKeyColumnsHelper(this,m_aMutex,aVector);
+}
+// -----------------------------------------------------------------------------
+
diff --git a/connectivity/source/commontools/TKeyColumns.cxx b/connectivity/source/commontools/TKeyColumns.cxx
new file mode 100644
index 000000000000..722c0015aaa6
--- /dev/null
+++ b/connectivity/source/commontools/TKeyColumns.cxx
@@ -0,0 +1,207 @@
+/*************************************************************************
+ *
+ * $RCSfile: TKeyColumns.cxx,v $
+ *
+ * $Revision: 1.1 $
+ *
+ * last change: $Author: oj $ $Date: 2002-10-25 09:01:21 $
+ *
+ * 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 CONNECTIVITY_TKEYCOLUMNS_HXX
+#include "connectivity/TKeyColumns.hxx"
+#endif
+#ifndef _CONNECTIVITY_SDBCX_KEYCOLUMN_HXX_
+#include "connectivity/sdbcx/VKeyColumn.hxx"
+#endif
+#ifndef _COM_SUN_STAR_SDBC_XROW_HPP_
+#include <com/sun/star/sdbc/XRow.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SDBC_XRESULTSET_HPP_
+#include <com/sun/star/sdbc/XResultSet.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SDBC_DATATYPE_HPP_
+#include <com/sun/star/sdbc/DataType.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SDBC_COLUMNVALUE_HPP_
+#include <com/sun/star/sdbc/ColumnValue.hpp>
+#endif
+#ifndef _COMPHELPER_EXTRACT_HXX_
+#include <comphelper/extract.hxx>
+#endif
+#ifndef _COMPHELPER_PROPERTY_HXX_
+#include <comphelper/property.hxx>
+#endif
+#ifndef CONNECTIVITY_CONNECTION_HXX
+#include "TConnection.hxx"
+#endif
+#ifndef CONNECTIVITY_TABLEHELPER_HXX
+#include "connectivity/TTableHelper.hxx"
+#endif
+
+using namespace connectivity;
+using namespace connectivity::sdbcx;
+using namespace ::com::sun::star::uno;
+using namespace ::com::sun::star::beans;
+// using namespace ::com::sun::star::sdbcx;
+using namespace ::com::sun::star::sdbc;
+using namespace ::com::sun::star::container;
+using namespace ::com::sun::star::lang;
+
+// -------------------------------------------------------------------------
+OKeyColumnsHelper::OKeyColumnsHelper( OTableKeyHelper* _pKey,
+ ::osl::Mutex& _rMutex,
+ const ::std::vector< ::rtl::OUString> &_rVector)
+ : connectivity::sdbcx::OCollection(*_pKey,sal_True,_rMutex,_rVector)
+ ,m_pKey(_pKey)
+{
+}
+// -------------------------------------------------------------------------
+Reference< XNamed > OKeyColumnsHelper::createObject(const ::rtl::OUString& _rName)
+{
+ ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
+ ::rtl::OUString aSchema,aTable;
+ m_pKey->getTable()->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_SCHEMANAME)) >>= aSchema;
+ m_pKey->getTable()->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)) >>= aTable;
+
+ // frist get the related column to _rName
+ Reference< XResultSet > xResult = m_pKey->getTable()->getMetaData()->getImportedKeys(
+ m_pKey->getTable()->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME)),aSchema,aTable);
+
+ ::rtl::OUString aRefColumnName;
+ if ( xResult.is() )
+ {
+ Reference< XRow > xRow(xResult,UNO_QUERY);
+ ::rtl::OUString aTemp;
+ while(xResult->next())
+ {
+ aTemp = xRow->getString(4);
+ if(xRow->getString(8) == _rName && m_pKey->getName() == xRow->getString(12))
+ {
+ aRefColumnName = aTemp;
+ break;
+ }
+ }
+ }
+
+ Reference< XNamed > xRet;
+
+ // now describe the column _rName and set his related column
+ xResult = m_pKey->getTable()->getMetaData()->getColumns(
+ m_pKey->getTable()->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME)),aSchema,aTable,_rName);
+
+ if ( xResult.is() )
+ {
+ Reference< XRow > xRow(xResult,UNO_QUERY);
+ if ( xResult->next() )
+ {
+ if ( xRow->getString(4) == _rName )
+ {
+ sal_Int32 nDataType = xRow->getInt(5);
+ ::rtl::OUString aTypeName(xRow->getString(6));
+ sal_Int32 nSize = xRow->getInt(7);
+ sal_Int32 nDec = xRow->getInt(9);
+ sal_Int32 nNull = xRow->getInt(11);
+ ::rtl::OUString sColumnDef;
+ try
+ {
+ sColumnDef = xRow->getString(13);
+ }
+ catch(const SQLException&)
+ {
+ // somethimes we get an error when asking for this param
+ }
+
+ OKeyColumn* pRet = new OKeyColumn(aRefColumnName,
+ _rName,
+ aTypeName,
+ sColumnDef,
+ nNull,
+ nSize,
+ nDec,
+ nDataType,
+ sal_False,
+ sal_False,
+ sal_False,
+ isCaseSensitive());
+ xRet = pRet;
+ }
+ }
+ }
+
+ return xRet;
+}
+// -------------------------------------------------------------------------
+Reference< XPropertySet > OKeyColumnsHelper::createEmptyObject()
+{
+ return new OKeyColumn(isCaseSensitive());
+}
+// -------------------------------------------------------------------------
+void OKeyColumnsHelper::impl_refresh() throw(::com::sun::star::uno::RuntimeException)
+{
+ m_pKey->refreshColumns();
+}
+// -----------------------------------------------------------------------------
+Reference< XNamed > OKeyColumnsHelper::cloneObject(const Reference< XPropertySet >& _xDescriptor)
+{
+ OKeyColumn* pColumn = new OKeyColumn(isCaseSensitive());
+ Reference<XPropertySet> xProp = pColumn;
+ ::comphelper::copyProperties(_xDescriptor,xProp);
+ Reference< XNamed > xName(xProp,UNO_QUERY);
+ OSL_ENSURE(xName.is(),"Must be a XName interface here !");
+ return xName;
+}
+// -----------------------------------------------------------------------------
+
+
diff --git a/connectivity/source/commontools/TKeys.cxx b/connectivity/source/commontools/TKeys.cxx
new file mode 100644
index 000000000000..595acaf7bea8
--- /dev/null
+++ b/connectivity/source/commontools/TKeys.cxx
@@ -0,0 +1,357 @@
+/*************************************************************************
+ *
+ * $RCSfile: TKeys.cxx,v $
+ *
+ * $Revision: 1.1 $
+ *
+ * last change: $Author: oj $ $Date: 2002-10-25 09:00:29 $
+ *
+ * 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 CONNECTIVITY_TKEYS_HXX
+#include "connectivity/TKeys.hxx"
+#endif
+
+#ifndef CONNECTIVITY_TKEY_HXX
+#include "connectivity/TKey.hxx"
+#endif
+#ifndef _COM_SUN_STAR_SDBC_XROW_HPP_
+#include <com/sun/star/sdbc/XRow.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SDBC_XRESULTSET_HPP_
+#include <com/sun/star/sdbc/XResultSet.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SDBCX_KEYTYPE_HPP_
+#include <com/sun/star/sdbcx/KeyType.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SDBC_KEYRULE_HPP_
+#include <com/sun/star/sdbc/KeyRule.hpp>
+#endif
+#ifndef _CONNECTIVITY_DBTOOLS_HXX_
+#include "connectivity/dbtools.hxx"
+#endif
+#ifndef _COMPHELPER_EXTRACT_HXX_
+#include <comphelper/extract.hxx>
+#endif
+#ifndef _COMPHELPER_TYPES_HXX_
+#include <comphelper/types.hxx>
+#endif
+#ifndef _COMPHELPER_PROPERTY_HXX_
+#include <comphelper/property.hxx>
+#endif
+#ifndef CONNECTIVITY_CONNECTION_HXX
+#include "TConnection.hxx"
+#endif
+
+namespace connectivity
+{
+using namespace comphelper;
+using namespace connectivity::sdbcx;
+using namespace dbtools;
+using namespace ::com::sun::star::uno;
+using namespace ::com::sun::star::beans;
+using namespace ::com::sun::star::sdbcx;
+using namespace ::com::sun::star::sdbc;
+using namespace ::com::sun::star::container;
+using namespace ::com::sun::star::lang;
+
+
+
+OKeysHelper::OKeysHelper( OTableHelper* _pTable,
+ ::osl::Mutex& _rMutex,
+ const TStringVector& _rVector
+ ) : OKeys_BASE(*_pTable,sal_True,_rMutex,_rVector,sal_True)
+ ,m_pTable(_pTable)
+{
+}
+// -------------------------------------------------------------------------
+Reference< XNamed > OKeysHelper::createObject(const ::rtl::OUString& _rName)
+{
+ Reference< XNamed > xRet = NULL;
+
+ if(_rName.getLength())
+ {
+ ::rtl::OUString aSchema,aTable;
+ ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
+ m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_SCHEMANAME)) >>= aSchema;
+ m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)) >>= aTable;
+
+ Reference< XResultSet > xResult = m_pTable->getMetaData()->getImportedKeys(m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME)),
+ aSchema,aTable);
+
+ if(xResult.is())
+ {
+ Reference< XRow > xRow(xResult,UNO_QUERY);
+ ::rtl::OUString aName,aCatalog;
+ while( xResult->next() )
+ {
+ // this must be outsid the "if" because we have to call in a right order
+ aCatalog = xRow->getString(1);
+ if ( xRow->wasNull() )
+ aCatalog = ::rtl::OUString();
+ aSchema = xRow->getString(2);
+ aName = xRow->getString(3);
+
+ sal_Int32 nUpdateRule = xRow->getInt(10);
+ sal_Int32 nDeleteRule = xRow->getInt(11);
+ if ( xRow->getString(12) == _rName )
+ {
+ ::rtl::OUString aComposedName;
+ ::dbtools::composeTableName(m_pTable->getMetaData(),aCatalog,aSchema,aName,aComposedName,sal_False,::dbtools::eInDataManipulation);
+ OTableKeyHelper* pRet = new OTableKeyHelper(m_pTable,_rName,aComposedName,KeyType::FOREIGN,nUpdateRule,nDeleteRule);
+ xRet = pRet;
+ break;
+ }
+ }
+ }
+ }
+
+ if(!xRet.is()) // we have a primary key with a system name
+ {
+ OTableKeyHelper* pRet = new OTableKeyHelper(m_pTable,_rName,::rtl::OUString(),KeyType::PRIMARY,KeyRule::NO_ACTION,KeyRule::NO_ACTION);
+ xRet = pRet;
+ }
+
+ return xRet;
+}
+// -------------------------------------------------------------------------
+void OKeysHelper::impl_refresh() throw(RuntimeException)
+{
+ m_pTable->refreshKeys();
+}
+// -------------------------------------------------------------------------
+Reference< XPropertySet > OKeysHelper::createEmptyObject()
+{
+ return new OTableKeyHelper(m_pTable);
+}
+// -----------------------------------------------------------------------------
+/** returns the keyrule string for the primary key
+*/
+::rtl::OUString getKeyRuleString(sal_Bool _bUpdate,sal_Int32 _nKeyRule)
+{
+ const char* pKeyRule = NULL;
+ switch ( _nKeyRule )
+ {
+ case KeyRule::CASCADE:
+ pKeyRule = _bUpdate ? " ON UPDATE CASCADE " : " ON DELETE CASCADE ";
+ break;
+ case KeyRule::RESTRICT:
+ pKeyRule = _bUpdate ? " ON UPDATE RESTRICT " : " ON DELETE RESTRICT ";
+ break;
+ case KeyRule::SET_NULL:
+ pKeyRule = _bUpdate ? " ON UPDATE SET NULL " : " ON DELETE SET NULL ";
+ break;
+ case KeyRule::SET_DEFAULT:
+ pKeyRule = _bUpdate ? " ON UPDATE SET DEFAULT " : " ON DELETE SET DEFAULT ";
+ break;
+ default:
+ ;
+ }
+ ::rtl::OUString sRet;
+ if ( pKeyRule )
+ sRet = ::rtl::OUString::createFromAscii(pKeyRule);
+ return sRet;
+}
+// -------------------------------------------------------------------------
+// XAppend
+void OKeysHelper::appendObject( const Reference< XPropertySet >& descriptor )
+{
+ ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
+ ::rtl::OUString aName = getString(descriptor->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)));
+ if ( !m_pTable->isNew() )
+ {
+ sal_Int32 nKeyType = getINT32(descriptor->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_TYPE)));
+
+ ::rtl::OUString aSql = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ALTER TABLE "));
+ ::rtl::OUString aQuote = m_pTable->getConnection()->getMetaData()->getIdentifierQuoteString( );
+ ::rtl::OUString aDot = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("."));
+
+ aSql += composeTableName(m_pTable->getConnection()->getMetaData(),m_pTable,sal_True,::dbtools::eInTableDefinitions) + ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" ADD "));
+
+ if ( nKeyType == KeyType::PRIMARY )
+ {
+ aSql += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" PRIMARY KEY ("));
+ }
+ else if ( nKeyType == KeyType::FOREIGN )
+ {
+ aSql += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" FOREIGN KEY ("));
+ }
+ else
+ throw SQLException();
+
+ Reference<XColumnsSupplier> xColumnSup(descriptor,UNO_QUERY);
+ Reference<XIndexAccess> xColumns(xColumnSup->getColumns(),UNO_QUERY);
+ Reference< XPropertySet > xColProp;
+ for(sal_Int32 i=0;i<xColumns->getCount();++i)
+ {
+ ::cppu::extractInterface(xColProp,xColumns->getByIndex(i));
+ aSql += ::dbtools::quoteName( aQuote,getString(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME))))
+ + ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(","));
+ }
+ aSql = aSql.replaceAt(aSql.getLength()-1,1,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(")")));
+
+ if ( nKeyType == KeyType::FOREIGN )
+ {
+ ::rtl::OUString aRefTable;
+
+ descriptor->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_REFERENCEDTABLE)) >>= aRefTable;
+
+ aSql += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" REFERENCES "))
+ + ::dbtools::quoteTableName(m_pTable->getConnection()->getMetaData(),aRefTable,::dbtools::eInTableDefinitions);
+ aSql += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" ("));
+
+ for(sal_Int32 i=0;i<xColumns->getCount();++i)
+ {
+ ::cppu::extractInterface(xColProp,xColumns->getByIndex(i));
+ aSql += ::dbtools::quoteName( aQuote,getString(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_RELATEDCOLUMN))))
+ + ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(","));
+ }
+ aSql = aSql.replaceAt(aSql.getLength()-1,1,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(")")));
+
+ aSql += getKeyRuleString(sal_True ,getINT32(descriptor->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_UPDATERULE))));
+ aSql += getKeyRuleString(sal_False ,getINT32(descriptor->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_DELETERULE))));
+ }
+
+ Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement( );
+ xStmt->execute(aSql);
+ // we need a name for the insertion
+ if ( nKeyType == KeyType::FOREIGN )
+ {
+ ::rtl::OUString aSchema,aTable;
+ ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
+ m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_SCHEMANAME)) >>= aSchema;
+ m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)) >>= aTable;
+
+ Reference< XResultSet > xResult = m_pTable->getMetaData()->getImportedKeys( m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME))
+ ,aSchema
+ ,aTable);
+ if ( xResult.is() )
+ {
+ Reference< XRow > xRow(xResult,UNO_QUERY);
+ while( xResult->next() )
+ {
+ ::rtl::OUString sName = xRow->getString(12);
+ ObjectMap::iterator aIter = m_aNameMap.find(sName);
+ if( aIter == m_aNameMap.end()) // this name wasn't inserted yet so it must be te new one
+ {
+ descriptor->setPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME),makeAny(sName));
+ break;
+ }
+ }
+ }
+ }
+ }
+}
+// -------------------------------------------------------------------------
+// XDrop
+void OKeysHelper::dropObject(sal_Int32 _nPos,const ::rtl::OUString _sElementName)
+{
+ if ( !m_pTable->isNew() )
+ {
+ ::rtl::OUString aSql = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ALTER TABLE "));
+
+ aSql += composeTableName(m_pTable->getConnection()->getMetaData(),m_pTable,sal_True,::dbtools::eInTableDefinitions);
+
+ ObjectIter aIter = m_aElements[_nPos];
+ if(!aIter->second.is()) // we want to drop a object which isn't loaded yet so we must load it
+ aIter->second = createObject(_sElementName);
+ Reference<XPropertySet> xKey(aIter->second,UNO_QUERY);
+ sal_Int32 nKeyType = 0;
+ ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
+ xKey->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_TYPE)) >>= nKeyType;
+
+ aSql += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" DROP CONSTRAINT "));
+ ::rtl::OUString aQuote = m_pTable->getConnection()->getMetaData()->getIdentifierQuoteString();
+ aSql += aQuote + _sElementName + aQuote;
+
+ Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement( );
+ if ( xStmt.is() )
+ {
+ xStmt->execute(aSql);
+ ::comphelper::disposeComponent(xStmt);
+ }
+ }
+}
+// -----------------------------------------------------------------------------
+Reference< XNamed > OKeysHelper::cloneObject(const Reference< XPropertySet >& _xDescriptor)
+{
+ Reference< XNamed > xName;
+ if(!m_pTable->isNew())
+ {
+ xName = Reference< XNamed >(_xDescriptor,UNO_QUERY);
+ OSL_ENSURE(xName.is(),"Must be a XName interface here !");
+ xName = xName.is() ? createObject(xName->getName()) : Reference< XNamed >();
+ }
+ else
+ {
+ Reference<XPropertySet> xProp = createEmptyObject();
+ ::comphelper::copyProperties(_xDescriptor,xProp);
+ Reference<XColumnsSupplier> xSup(_xDescriptor,UNO_QUERY);
+ Reference<XIndexAccess> xIndex(xSup->getColumns(),UNO_QUERY);
+ Reference<XColumnsSupplier> xDestSup(xProp,UNO_QUERY);
+ Reference<XAppend> xAppend(xDestSup->getColumns(),UNO_QUERY);
+ sal_Int32 nCount = xIndex->getCount();
+ for(sal_Int32 i=0;i< nCount;++i)
+ {
+ Reference<XPropertySet> xColProp;
+ xIndex->getByIndex(i) >>= xColProp;
+ xAppend->appendByDescriptor(xColProp);
+ }
+ xName = Reference< XNamed >(xProp,UNO_QUERY);
+ }
+ return xName;
+}
+// -----------------------------------------------------------------------------
+} // namespace connectivity
+// -----------------------------------------------------------------------------
diff --git a/connectivity/source/commontools/TTableHelper.cxx b/connectivity/source/commontools/TTableHelper.cxx
new file mode 100644
index 000000000000..d645cbd881b3
--- /dev/null
+++ b/connectivity/source/commontools/TTableHelper.cxx
@@ -0,0 +1,368 @@
+/*************************************************************************
+ *
+ * $RCSfile: TTableHelper.cxx,v $
+ *
+ * $Revision: 1.1 $
+ *
+ * last change: $Author: oj $ $Date: 2002-10-25 09:00:57 $
+ *
+ * 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 CONNECTIVITY_TABLEHELPER_HXX
+#include "connectivity/TTableHelper.hxx"
+#endif
+#ifndef _COM_SUN_STAR_SDBC_XROW_HPP_
+#include <com/sun/star/sdbc/XRow.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SDBC_XRESULTSET_HPP_
+#include <com/sun/star/sdbc/XResultSet.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SDBCX_KEYTYPE_HPP_
+#include <com/sun/star/sdbcx/KeyType.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SDBC_KEYRULE_HPP_
+#include <com/sun/star/sdbc/KeyRule.hpp>
+#endif
+#ifndef _CPPUHELPER_TYPEPROVIDER_HXX_
+#include <cppuhelper/typeprovider.hxx>
+#endif
+#ifndef _COM_SUN_STAR_LANG_DISPOSEDEXCEPTION_HPP_
+#include <com/sun/star/lang/DisposedException.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SDBC_COLUMNVALUE_HPP_
+#include <com/sun/star/sdbc/ColumnValue.hpp>
+#endif
+#ifndef _COMPHELPER_SEQUENCE_HXX_
+#include <comphelper/sequence.hxx>
+#endif
+#ifndef _COMPHELPER_EXTRACT_HXX_
+#include <comphelper/extract.hxx>
+#endif
+#ifndef _COMPHELPER_TYPES_HXX_
+#include <comphelper/types.hxx>
+#endif
+#ifndef _CONNECTIVITY_DBTOOLS_HXX_
+#include "connectivity/dbtools.hxx"
+#endif
+#ifndef _CONNECTIVITY_SDBCX_COLLECTION_HXX_
+#include "connectivity/sdbcx/VCollection.hxx"
+#endif
+#ifndef CONNECTIVITY_CONNECTION_HXX
+#include "TConnection.hxx"
+#endif
+
+using namespace ::comphelper;
+using namespace connectivity;
+using namespace ::com::sun::star::uno;
+using namespace ::com::sun::star::beans;
+using namespace ::com::sun::star::sdbcx;
+using namespace ::com::sun::star::sdbc;
+using namespace ::com::sun::star::container;
+using namespace ::com::sun::star::lang;
+
+OTableHelper::OTableHelper( sdbcx::OCollection* _pTables,
+ const Reference< XConnection >& _xConnection,
+ sal_Bool _bCase)
+ :OTable_TYPEDEF(_pTables,_bCase)
+ ,m_xConnection(_xConnection)
+{
+ try
+ {
+ m_xMetaData = m_xConnection->getMetaData();
+ }
+ catch(const Exception&)
+ {
+ }
+}
+// -------------------------------------------------------------------------
+OTableHelper::OTableHelper( sdbcx::OCollection* _pTables,
+ const Reference< XConnection >& _xConnection,
+ sal_Bool _bCase,
+ const ::rtl::OUString& _Name,
+ const ::rtl::OUString& _Type,
+ const ::rtl::OUString& _Description ,
+ const ::rtl::OUString& _SchemaName,
+ const ::rtl::OUString& _CatalogName
+ ) : OTable_TYPEDEF(_pTables,
+ _bCase,
+ _Name,
+ _Type,
+ _Description,
+ _SchemaName,
+ _CatalogName)
+ ,m_xConnection(_xConnection)
+{
+ try
+ {
+ m_xMetaData = m_xConnection->getMetaData();
+ }
+ catch(const Exception&)
+ {
+ }
+}
+// -----------------------------------------------------------------------------
+void SAL_CALL OTableHelper::disposing()
+{
+ OTable_TYPEDEF::disposing();
+ ::osl::MutexGuard aGuard(m_aMutex);
+ m_xConnection = NULL;
+ m_xMetaData = NULL;
+}
+// -------------------------------------------------------------------------
+void OTableHelper::refreshColumns()
+{
+ TStringVector aVector;
+ if(!isNew())
+ {
+ Any aCatalog;
+ if ( m_CatalogName.getLength() )
+ aCatalog <<= m_CatalogName;
+ Reference< XResultSet > xResult = getMetaData()->getColumns( aCatalog,
+ 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));
+ ::comphelper::disposeComponent(xResult);
+ }
+ }
+
+ if(m_pColumns)
+ m_pColumns->reFill(aVector);
+ else
+ m_pColumns = createColumns(aVector);
+}
+// -------------------------------------------------------------------------
+void OTableHelper::refreshPrimaryKeys(std::vector< ::rtl::OUString>& _rKeys)
+{
+ Any aCatalog;
+ if ( m_CatalogName.getLength() )
+ aCatalog <<= m_CatalogName;
+ Reference< XResultSet > xResult = getMetaData()->getPrimaryKeys(aCatalog,m_SchemaName,m_Name);
+
+ if(xResult.is())
+ {
+ Reference< XRow > xRow(xResult,UNO_QUERY);
+ if(xResult->next()) // there can be only one primary key
+ {
+ ::rtl::OUString aPkName = xRow->getString(6);
+ _rKeys.push_back(aPkName);
+ }
+ ::comphelper::disposeComponent(xResult);
+ }
+}
+// -------------------------------------------------------------------------
+void OTableHelper::refreshForgeinKeys(std::vector< ::rtl::OUString>& _rKeys)
+{
+ Any aCatalog;
+ if ( m_CatalogName.getLength() )
+ aCatalog <<= m_CatalogName;
+ Reference< XResultSet > xResult = getMetaData()->getImportedKeys(aCatalog,m_SchemaName,m_Name);
+ Reference< XRow > xRow(xResult,UNO_QUERY);
+
+ if ( xRow.is() )
+ {
+ 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);
+ }
+ }
+ ::comphelper::disposeComponent(xResult);
+ }
+}
+// -------------------------------------------------------------------------
+void OTableHelper::refreshKeys()
+{
+ TStringVector aVector;
+
+ if(!isNew())
+ {
+ refreshPrimaryKeys(aVector);
+ refreshForgeinKeys(aVector);
+ }
+ if(m_pKeys)
+ m_pKeys->reFill(aVector);
+ else
+ m_pKeys = createKeys(aVector);
+}
+// -------------------------------------------------------------------------
+void OTableHelper::refreshIndexes()
+{
+ TStringVector aVector;
+ if(!isNew())
+ {
+ // fill indexes
+ Any aCatalog;
+ if ( m_CatalogName.getLength() )
+ aCatalog <<= m_CatalogName;
+ Reference< XResultSet > xResult = getMetaData()->getIndexInfo(aCatalog,m_SchemaName,m_Name,sal_False,sal_False);
+
+ if(xResult.is())
+ {
+ Reference< XRow > xRow(xResult,UNO_QUERY);
+ ::rtl::OUString aName;
+ ::rtl::OUString sCatalogSep = getMetaData()->getCatalogSeparator();
+ ::rtl::OUString sPreviousRoundName;
+ while( xResult->next() )
+ {
+ aName = xRow->getString(5);
+ if(aName.getLength())
+ aName += sCatalogSep;
+ aName += xRow->getString(6);
+ if ( aName.getLength() )
+ {
+ // don't insert the name if the last one we inserted was the same
+ if (sPreviousRoundName != aName)
+ aVector.push_back(aName);
+ }
+ sPreviousRoundName = aName;
+ }
+ ::comphelper::disposeComponent(xResult);
+ }
+ }
+
+ if(m_pIndexes)
+ m_pIndexes->reFill(aVector);
+ else
+ m_pIndexes = createIndexes(aVector);
+}
+// -------------------------------------------------------------------------
+// XRename
+void SAL_CALL OTableHelper::rename( const ::rtl::OUString& newName ) throw(SQLException, ElementExistException, RuntimeException)
+{
+ ::osl::MutexGuard aGuard(m_aMutex);
+ checkDisposed(
+#ifdef GCC
+ ::connectivity::sdbcx::OTableDescriptor_BASE::rBHelper.bDisposed
+#else
+ rBHelper.bDisposed
+#endif
+ );
+
+ if(!isNew())
+ {
+ ::rtl::OUString sSql = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("RENAME "));
+ if ( m_Type == ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("VIEW")) )
+ sSql += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" VIEW "));
+ else
+ sSql += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" TABLE "));
+
+ ::rtl::OUString sQuote = getMetaData()->getIdentifierQuoteString( );
+
+ ::rtl::OUString sCatalog,sSchema,sTable;
+ ::dbtools::qualifiedNameComponents(getMetaData(),newName,sCatalog,sSchema,sTable,::dbtools::eInDataManipulation);
+
+ ::rtl::OUString sComposedName;
+ ::dbtools::composeTableName(getMetaData(),m_CatalogName,m_SchemaName,m_Name,sComposedName,sal_True,::dbtools::eInDataManipulation);
+ sSql += sComposedName
+ + ::rtl::OUString::createFromAscii(" TO ");
+ ::dbtools::composeTableName(getMetaData(),sCatalog,sSchema,sTable,sComposedName,sal_True,::dbtools::eInDataManipulation);
+ sSql += sComposedName;
+
+ Reference< XStatement > xStmt = m_xConnection->createStatement( );
+ if ( xStmt.is() )
+ {
+ xStmt->execute(sSql);
+ ::comphelper::disposeComponent(xStmt);
+ }
+ OTable_TYPEDEF::rename(newName);
+ }
+ else
+ ::dbtools::qualifiedNameComponents(getMetaData(),newName,m_CatalogName,m_SchemaName,m_Name,::dbtools::eInTableDefinitions);
+}
+// -------------------------------------------------------------------------
+void SAL_CALL OTableHelper::alterColumnByIndex( sal_Int32 index, const Reference< XPropertySet >& descriptor ) throw(SQLException, ::com::sun::star::lang::IndexOutOfBoundsException, RuntimeException)
+{
+ ::osl::MutexGuard aGuard(m_aMutex);
+ checkDisposed(
+#ifdef GCC
+ ::connectivity::sdbcx::OTableDescriptor_BASE::rBHelper.bDisposed
+#else
+ rBHelper.bDisposed
+#endif
+ );
+
+ Reference< XPropertySet > xOld;
+ if(::cppu::extractInterface(xOld,m_pColumns->getByIndex(index)) && xOld.is())
+ alterColumnByName(getString(xOld->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME))),descriptor);
+}
+
+// -------------------------------------------------------------------------
+::rtl::OUString SAL_CALL OTableHelper::getName() throw(RuntimeException)
+{
+ ::rtl::OUString sComposedName;
+ ::dbtools::composeTableName(getMetaData(),m_CatalogName,m_SchemaName,m_Name,sComposedName,sal_False,::dbtools::eInDataManipulation);
+ return sComposedName;
+}
+// -----------------------------------------------------------------------------
+void SAL_CALL OTableHelper::acquire() throw()
+{
+ OTable_TYPEDEF::acquire();
+}
+// -----------------------------------------------------------------------------
+void SAL_CALL OTableHelper::release() throw()
+{
+ OTable_TYPEDEF::release();
+}
+// -----------------------------------------------------------------------------
+
diff --git a/connectivity/source/commontools/dbtools2.cxx b/connectivity/source/commontools/dbtools2.cxx
new file mode 100644
index 000000000000..2f84ac9a71fc
--- /dev/null
+++ b/connectivity/source/commontools/dbtools2.cxx
@@ -0,0 +1,603 @@
+/*************************************************************************
+ *
+ * $RCSfile: dbtools2.cxx,v $
+ *
+ * $Revision: 1.1 $
+ *
+ * last change: $Author: oj $ $Date: 2002-10-25 09: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 _CONNECTIVITY_DBTOOLS_HXX_
+#include "connectivity/dbtools.hxx"
+#endif
+#ifndef _COM_SUN_STAR_SDBC_XCONNECTION_HPP_
+#include <com/sun/star/sdbc/XConnection.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SDBC_COLUMNVALUE_HPP_
+#include <com/sun/star/sdbc/ColumnValue.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SDBC_XROW_HPP_
+#include <com/sun/star/sdbc/XRow.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SDBC_XRESULTSETMETADATASUPPLIER_HPP_
+#include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SDBCX_XKEYSSUPPLIER_HPP_
+#include <com/sun/star/sdbcx/XKeysSupplier.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SDBC_XDRIVERACCESS_HPP_
+#include <com/sun/star/sdbc/XDriverAccess.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SDBCX_XDATADEFINITIONSUPPLIER_HPP_
+#include <com/sun/star/sdbcx/XDataDefinitionSupplier.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SDBCX_PRIVILEGE_HPP_
+#include <com/sun/star/sdbcx/Privilege.hpp>
+#endif
+#ifndef _COM_SUN_STAR_CONTAINER_XINDEXACCESS_HPP_
+#include <com/sun/star/container/XIndexAccess.hpp>
+#endif
+#ifndef _COM_SUN_STAR_CONTAINER_XENUMERATIONACCESS_HPP_
+#include <com/sun/star/container/XEnumerationAccess.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SDBC_KEYRULE_HPP_
+#include <com/sun/star/sdbc/KeyRule.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SDBCX_KEYTYPE_HPP_
+#include <com/sun/star/sdbcx/KeyType.hpp>
+#endif
+#ifndef CONNECTIVITY_CONNECTION_HXX
+#include "TConnection.hxx"
+#endif
+#ifndef _CONNECTIVITY_SDBCX_COLUMN_HXX_
+#include "connectivity/sdbcx/VColumn.hxx"
+#endif
+
+//.........................................................................
+namespace dbtools
+{
+//.........................................................................
+ using namespace ::com::sun::star::uno;
+ using namespace ::com::sun::star::beans;
+ using namespace ::com::sun::star::sdbc;
+ using namespace ::com::sun::star::sdbcx;
+ using namespace ::com::sun::star::lang;
+ using namespace ::com::sun::star::container;
+ using namespace connectivity;
+ using namespace comphelper;
+
+::rtl::OUString createStandardColumnPart(const Reference< XPropertySet >& xColProp,const Reference< XConnection>& _xConnection)
+{
+
+ Reference<XDatabaseMetaData> xMetaData = _xConnection->getMetaData();
+
+ ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
+
+ ::rtl::OUString sTypeName;
+ sal_Int32 nDataType = 0;
+ sal_Int32 nPrecision = 0;
+ sal_Int32 nScale = 0;
+
+ ::rtl::OUString aSql = ::dbtools::quoteTableName(xMetaData,::comphelper::getString(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME))),::dbtools::eInTableDefinitions);
+
+ aSql += ::rtl::OUString::createFromAscii(" ");
+
+ nDataType = nPrecision = nScale = 0;
+ sal_Bool bIsAutoIncrement = sal_False;
+ xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_TYPENAME)) >>= sTypeName;
+ xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_TYPE)) >>= nDataType;
+ xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_PRECISION)) >>= nPrecision;
+ xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_SCALE)) >>= nScale;
+ xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_ISAUTOINCREMENT)) >>= bIsAutoIncrement;
+ // look if we have to use precisions
+ sal_Bool bUseLiteral = sal_False;
+ ::rtl::OUString sPreFix,sPostFix;
+ {
+ Reference<XResultSet> xRes = xMetaData->getTypeInfo();
+ if(xRes.is())
+ {
+ Reference<XRow> xRow(xRes,UNO_QUERY);
+ while(xRes->next())
+ {
+ ::rtl::OUString sTypeName2Cmp = xRow->getString(1);
+ sal_Int32 nType = xRow->getShort(2);
+ sPreFix = xRow->getString (4);
+ sPostFix = xRow->getString (5);
+ ::rtl::OUString sCreateParams = xRow->getString(6);
+ // first identical type will be used if typename is empty
+ if ( !sTypeName.getLength() && nType == nDataType )
+ sTypeName = sTypeName2Cmp;
+
+ if( sTypeName.equalsIgnoreAsciiCase(sTypeName2Cmp) && nType == nDataType && sCreateParams.getLength() && !xRow->wasNull())
+ {
+ bUseLiteral = sal_True;
+ break;
+ }
+ }
+ }
+ }
+
+ aSql += sTypeName;
+ if(nPrecision > 0 && bUseLiteral)
+ {
+ aSql += ::rtl::OUString::createFromAscii("(");
+ aSql += ::rtl::OUString::valueOf(nPrecision);
+ if(nScale > 0)
+ {
+ aSql += ::rtl::OUString::createFromAscii(",");
+ aSql += ::rtl::OUString::valueOf(nScale);
+ }
+ aSql += ::rtl::OUString::createFromAscii(")");
+ }
+
+ ::rtl::OUString aDefault = ::comphelper::getString(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_DEFAULTVALUE)));
+ if(aDefault.getLength())
+ aSql += ::rtl::OUString::createFromAscii(" DEFAULT ") + sPreFix + aDefault + sPostFix;
+
+ if(::comphelper::getINT32(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_ISNULLABLE))) == ColumnValue::NO_NULLS)
+ aSql += ::rtl::OUString::createFromAscii(" NOT NULL");
+
+ if ( bIsAutoIncrement )
+ {
+ // check if the user enter a specific string to create autoincrement values
+ Reference<XPropertySetInfo> xPropInfo = xColProp->getPropertySetInfo();
+ if ( xPropInfo->hasPropertyByName(rPropMap.getNameByIndex(PROPERTY_ID_AUTOINCREMENTCREATION)) )
+ {
+ ::rtl::OUString sAutoIncrementValue;
+ xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_AUTOINCREMENTCREATION)) >>= sAutoIncrementValue;
+ if ( sAutoIncrementValue.getLength() )
+ {
+ aSql += ::rtl::OUString::createFromAscii(" ");
+ aSql += sAutoIncrementValue;
+ }
+ }
+ }
+
+ return aSql;
+}
+// -----------------------------------------------------------------------------
+
+::rtl::OUString createStandardCreateStatement(const Reference< XPropertySet >& descriptor,const Reference< XConnection>& _xConnection)
+{
+ ::rtl::OUString aSql = ::rtl::OUString::createFromAscii("CREATE TABLE ");
+ ::rtl::OUString sCatalog,sSchema,sTable,sComposedName;
+
+ Reference<XDatabaseMetaData> xMetaData = _xConnection->getMetaData();
+ ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
+
+ descriptor->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME)) >>= sCatalog;
+ descriptor->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_SCHEMANAME)) >>= sSchema;
+ descriptor->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)) >>= sTable;
+
+ ::dbtools::composeTableName(xMetaData,sCatalog,sSchema,sTable,sComposedName,sal_True,::dbtools::eInTableDefinitions);
+ if ( !sComposedName.getLength() )
+ ::dbtools::throwFunctionSequenceException(_xConnection);
+
+ aSql += sComposedName + ::rtl::OUString::createFromAscii(" (");
+
+ // columns
+ Reference<XColumnsSupplier> xColumnSup(descriptor,UNO_QUERY);
+ Reference<XIndexAccess> xColumns(xColumnSup->getColumns(),UNO_QUERY);
+ // check if there are columns
+ if(!xColumns.is() || !xColumns->getCount())
+ ::dbtools::throwFunctionSequenceException(_xConnection);
+
+ Reference< XPropertySet > xColProp;
+
+ sal_Int32 nCount = xColumns->getCount();
+ for(sal_Int32 i=0;i<nCount;++i)
+ {
+ if ( (xColumns->getByIndex(i) >>= xColProp) && xColProp.is() )
+ {
+ aSql += createStandardColumnPart(xColProp,_xConnection);
+ aSql += ::rtl::OUString::createFromAscii(",");
+ }
+ }
+ return aSql;
+}
+// -----------------------------------------------------------------------------
+::rtl::OUString createStandardKeyStatement(const Reference< XPropertySet >& descriptor,const Reference< XConnection>& _xConnection)
+{
+ Reference<XDatabaseMetaData> xMetaData = _xConnection->getMetaData();
+ ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
+
+ ::rtl::OUString aSql;
+ // keys
+ Reference<XKeysSupplier> xKeySup(descriptor,UNO_QUERY);
+ Reference<XIndexAccess> xKeys = xKeySup->getKeys();
+ if ( xKeys.is() )
+ {
+ Reference< XPropertySet > xColProp;
+ Reference<XIndexAccess> xColumns;
+ Reference<XColumnsSupplier> xColumnSup;
+ ::rtl::OUString sCatalog,sSchema,sTable,sComposedName;
+ sal_Bool bPKey = sal_False;
+ for(sal_Int32 i=0;i<xKeys->getCount();++i)
+ {
+ if ( (xKeys->getByIndex(i) >>= xColProp) && xColProp.is() )
+ {
+
+ sal_Int32 nKeyType = ::comphelper::getINT32(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_TYPE)));
+
+ if ( nKeyType == KeyType::PRIMARY )
+ {
+ if(bPKey)
+ ::dbtools::throwFunctionSequenceException(_xConnection);
+
+ bPKey = sal_True;
+ xColumnSup = Reference<XColumnsSupplier>(xColProp,UNO_QUERY);
+ xColumns = Reference<XIndexAccess>(xColumnSup->getColumns(),UNO_QUERY);
+ if(!xColumns.is() || !xColumns->getCount())
+ ::dbtools::throwFunctionSequenceException(_xConnection);
+
+ aSql += ::rtl::OUString::createFromAscii(" PRIMARY KEY (");
+ for(sal_Int32 i=0;i<xColumns->getCount();++i)
+ {
+ if ( (xColumns->getByIndex(i) >>= xColProp) && xColProp.is() )
+ aSql += ::dbtools::quoteTableName(xMetaData,::comphelper::getString(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME))),::dbtools::eInTableDefinitions)
+ + ::rtl::OUString::createFromAscii(",");
+ }
+
+ aSql = aSql.replaceAt(aSql.getLength()-1,1,::rtl::OUString::createFromAscii(")"));
+ }
+ else if(nKeyType == KeyType::UNIQUE)
+ {
+ xColumnSup = Reference<XColumnsSupplier>(xColProp,UNO_QUERY);
+ xColumns = Reference<XIndexAccess>(xColumnSup->getColumns(),UNO_QUERY);
+ if(!xColumns.is() || !xColumns->getCount())
+ ::dbtools::throwFunctionSequenceException(_xConnection);
+
+ aSql += ::rtl::OUString::createFromAscii(" UNIQUE (");
+ for(sal_Int32 i=0;i<xColumns->getCount();++i)
+ {
+ if ( (xColumns->getByIndex(i) >>= xColProp) && xColProp.is() )
+ aSql += ::dbtools::quoteTableName(xMetaData,::comphelper::getString(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME))),::dbtools::eInTableDefinitions)
+ + ::rtl::OUString::createFromAscii(",");
+ }
+
+ aSql = aSql.replaceAt(aSql.getLength()-1,1,::rtl::OUString::createFromAscii(")"));
+ }
+ else if(nKeyType == KeyType::FOREIGN)
+ {
+ sal_Int32 nDeleteRule = getINT32(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_DELETERULE)));
+
+ xColumnSup = Reference<XColumnsSupplier>(xColProp,UNO_QUERY);
+ xColumns = Reference<XIndexAccess>(xColumnSup->getColumns(),UNO_QUERY);
+ if(!xColumns.is() || !xColumns->getCount())
+ ::dbtools::throwFunctionSequenceException(_xConnection);
+
+ aSql += ::rtl::OUString::createFromAscii(" FOREIGN KEY ");
+ ::rtl::OUString sRefTable = getString(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_REFERENCEDTABLE)));
+ ::dbtools::qualifiedNameComponents(xMetaData,
+ sRefTable,
+ sCatalog,
+ sSchema,
+ sTable,
+ ::dbtools::eInDataManipulation);
+ ::dbtools::composeTableName(xMetaData,sCatalog, sSchema, sTable,sComposedName,sal_True,::dbtools::eInTableDefinitions);
+
+
+ if(!sComposedName.getLength())
+ ::dbtools::throwFunctionSequenceException(_xConnection);
+ aSql += sComposedName + ::rtl::OUString::createFromAscii(" (");
+
+ sal_Int32 nColCount = xColumns->getCount();
+ for(sal_Int32 i=0;i<nColCount;++i)
+ {
+ if ( (xColumns->getByIndex(i) >>= xColProp) && xColProp.is() )
+ aSql += ::dbtools::quoteTableName(xMetaData,::comphelper::getString(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME))),::dbtools::eInTableDefinitions)
+ + ::rtl::OUString::createFromAscii(",");
+ }
+
+ aSql = aSql.replaceAt(aSql.getLength()-1,1,::rtl::OUString::createFromAscii(")"));
+
+ switch(nDeleteRule)
+ {
+ case KeyRule::CASCADE:
+ aSql += ::rtl::OUString::createFromAscii(" ON DELETE CASCADE ");
+ break;
+ case KeyRule::RESTRICT:
+ aSql += ::rtl::OUString::createFromAscii(" ON DELETE RESTRICT ");
+ break;
+ case KeyRule::SET_NULL:
+ aSql += ::rtl::OUString::createFromAscii(" ON DELETE SET NULL ");
+ break;
+ case KeyRule::SET_DEFAULT:
+ aSql += ::rtl::OUString::createFromAscii(" ON DELETE SET DEFAULT ");
+ break;
+ default:
+ ;
+ }
+ }
+ }
+ }
+ }
+
+ if(aSql.lastIndexOf(',') == (aSql.getLength()-1))
+ aSql = aSql.replaceAt(aSql.getLength()-1,1,::rtl::OUString::createFromAscii(")"));
+ else
+ aSql += ::rtl::OUString::createFromAscii(")");
+
+ return aSql;
+
+}
+// -----------------------------------------------------------------------------
+Reference<XPropertySet> createSDBCXColumn(const Reference<XPropertySet>& _xTable,
+ const Reference<XConnection>& _xConnection,
+ const ::rtl::OUString& _rName,
+ sal_Bool _bCase)
+{
+ Reference<XPropertySet> xProp;
+ OSL_ENSURE(_xTable.is(),"Table is NULL!");
+ if ( !_xTable.is() )
+ return xProp;
+
+ ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
+ Reference<XDatabaseMetaData> xMetaData = _xConnection->getMetaData();
+ Any aCatalog;
+ aCatalog = _xTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME));
+
+ ::rtl::OUString aSchema, aTable;
+ _xTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_SCHEMANAME)) >>= aSchema;
+ _xTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)) >>= aTable;
+
+ Reference< XResultSet > xResult = 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 = xMetaData->getIdentifierQuoteString();
+
+ ::rtl::OUString sSelect = ::rtl::OUString::createFromAscii("SELECT ");
+ sSelect += ::dbtools::quoteName(sQuote,_rName);
+ ::rtl::OUString sComposedName;
+ ::dbtools::composeTableName(xMetaData,getString(aCatalog),aSchema,aTable,sComposedName,sal_True,::dbtools::eInDataManipulation);
+
+ 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 = _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&)
+ {
+ }
+
+
+ connectivity::sdbcx::OColumn* pRet = new connectivity::sdbcx::OColumn(_rName,
+ aField6,
+ sField13,
+ nField11,
+ nField7,
+ nField9,
+ nField5,
+ bAutoIncrement,
+ sal_False,
+ bIsCurrency,
+ _bCase);
+
+ xProp = pRet;
+ break;
+ }
+ }
+ }
+ return xProp;
+}
+// -----------------------------------------------------------------------------
+::rtl::OUString composeTableName(const Reference<XDatabaseMetaData>& _xMetaData,
+ const Reference<XPropertySet>& _xTable,
+ sal_Bool _bQuote,
+ EComposeRule _eComposeRule)
+{
+ ::rtl::OUString aCatalog;
+ ::rtl::OUString aSchema;
+ ::rtl::OUString aTable;
+ ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
+ _xTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME)) >>= aCatalog;
+ _xTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_SCHEMANAME)) >>= aSchema;
+ _xTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)) >>= aTable;
+
+ ::rtl::OUString aComposedName;
+ dbtools::composeTableName(_xMetaData,aCatalog,aSchema,aTable,aComposedName,_bQuote,_eComposeRule);
+ return aComposedName;
+}
+// -----------------------------------------------------------------------------
+Reference< XTablesSupplier> getDataDefinitionByURLAndConnection(
+ const ::rtl::OUString& _rsUrl,
+ const Reference< XConnection>& _xConnection,
+ const Reference< XMultiServiceFactory>& _rxFactory)
+{
+ Reference< XDriverAccess> xManager(_rxFactory->createInstance(::rtl::OUString::createFromAscii("com.sun.star.sdbc.DriverManager") ), UNO_QUERY);
+ Reference< XDataDefinitionSupplier > xSupp(xManager->getDriverByURL(_rsUrl),UNO_QUERY);
+ Reference< XTablesSupplier> xTablesSup;
+
+ if ( xSupp.is() )
+ xTablesSup = xSupp->getDataDefinitionByConnection(_xConnection);
+ // if we don't get the catalog from the original driver we have to try them all.
+ if ( !xTablesSup.is() )
+ {
+ Reference< XEnumerationAccess> xEnumAccess(xManager,UNO_QUERY);
+ Reference< XEnumeration> xEnum = xEnumAccess->createEnumeration();
+ while ( xEnum.is() && xEnum->hasMoreElements() && !xTablesSup.is() )
+ {
+ xEnum->nextElement() >>= xSupp;
+ if ( xSupp.is() )
+ xTablesSup = xSupp->getDataDefinitionByConnection(_xConnection);
+ }
+ }
+ return xTablesSup;
+}
+// -----------------------------------------------------------------------------
+sal_Int32 getTablePrivileges(const Reference< XDatabaseMetaData>& _xMetaData,
+ const ::rtl::OUString& _sCatalog,
+ const ::rtl::OUString& _sSchema,
+ const ::rtl::OUString& _sTable)
+{
+ OSL_ENSURE(_xMetaData.is(),"Invalid metadata!");
+ sal_Int32 nPrivileges = 0;
+ try
+ {
+ Any aVal;
+ if(_sCatalog.getLength())
+ aVal <<= _sCatalog;
+ Reference< XResultSet > xPrivileges = _xMetaData->getTablePrivileges(aVal, _sSchema, _sTable);
+ Reference< XRow > xCurrentRow(xPrivileges, UNO_QUERY);
+
+ if ( xCurrentRow.is() )
+ {
+ ::rtl::OUString sUserWorkingFor = _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");
+ // after creation the set is positioned before the first record, per definitionem
+
+ ::rtl::OUString sPrivilege, sGrantee;
+ while ( xPrivileges->next() )
+ {
+#ifdef DBG_UTIL
+ ::rtl::OUString sCat, sSchema, sName, sGrantor, sGrantable;
+ sCat = xCurrentRow->getString(1);
+ sSchema = xCurrentRow->getString(2);
+ sName = xCurrentRow->getString(3);
+ sGrantor = xCurrentRow->getString(4);
+#endif
+ sGrantee = xCurrentRow->getString(5);
+ sPrivilege = xCurrentRow->getString(6);
+#ifdef DBG_UTIL
+ sGrantable = xCurrentRow->getString(7);
+#endif
+
+ if (!sUserWorkingFor.equalsIgnoreAsciiCase(sGrantee))
+ continue;
+
+ if (sPrivilege.equalsIgnoreAsciiCase(sSELECT))
+ nPrivileges |= Privilege::SELECT;
+ else if (sPrivilege.equalsIgnoreAsciiCase(sINSERT))
+ nPrivileges |= Privilege::INSERT;
+ else if (sPrivilege.equalsIgnoreAsciiCase(sUPDATE))
+ nPrivileges |= Privilege::UPDATE;
+ else if (sPrivilege.equalsIgnoreAsciiCase(sDELETE))
+ nPrivileges |= Privilege::DELETE;
+ else if (sPrivilege.equalsIgnoreAsciiCase(sREAD))
+ nPrivileges |= Privilege::READ;
+ else if (sPrivilege.equalsIgnoreAsciiCase(sCREATE))
+ nPrivileges |= Privilege::CREATE;
+ else if (sPrivilege.equalsIgnoreAsciiCase(sALTER))
+ nPrivileges |= Privilege::ALTER;
+ else if (sPrivilege.equalsIgnoreAsciiCase(sREFERENCE))
+ nPrivileges |= Privilege::REFERENCE;
+ else if (sPrivilege.equalsIgnoreAsciiCase(sDROP))
+ nPrivileges |= Privilege::DROP;
+ }
+ }
+ disposeComponent(xPrivileges);
+ }
+ catch(const 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)
+ nPrivileges |= Privilege::DROP |
+ Privilege::REFERENCE |
+ Privilege::ALTER |
+ Privilege::CREATE |
+ Privilege::READ |
+ Privilege::DELETE |
+ Privilege::UPDATE |
+ Privilege::INSERT |
+ Privilege::SELECT;
+ else
+ OSL_ENSURE(0,"Could not collect the privileges !");
+ }
+ return nPrivileges;
+}
+//.........................................................................
+} // namespace dbtools
+//.........................................................................
+