merge the formfield patch from ooo-build
[ooovba.git] / connectivity / source / commontools / TColumnsHelper.cxx
blob032d8595754383e1c08031132570912446d41675
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: TColumnsHelper.cxx,v $
10 * $Revision: 1.11 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
33 #include "connectivity/TColumnsHelper.hxx"
34 #include "connectivity/sdbcx/VColumn.hxx"
35 #include "connectivity/sdbcx/VColumn.hxx"
36 #include <com/sun/star/sdbc/XRow.hpp>
37 #include <com/sun/star/sdbc/XResultSet.hpp>
38 #include <com/sun/star/sdbc/DataType.hpp>
39 #include <com/sun/star/sdbc/ColumnValue.hpp>
40 #include <com/sun/star/sdbcx/KeyType.hpp>
41 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
42 #include <com/sun/star/sdbcx/XKeysSupplier.hpp>
43 #include <comphelper/types.hxx>
44 #include "connectivity/dbtools.hxx"
45 #include "TConnection.hxx"
46 #include "connectivity/TTableHelper.hxx"
47 #include <comphelper/property.hxx>
49 using namespace ::comphelper;
52 using namespace connectivity::sdbcx;
53 using namespace connectivity;
54 using namespace dbtools;
55 using namespace ::com::sun::star::uno;
56 using namespace ::com::sun::star::beans;
57 using namespace ::com::sun::star::sdbcx;
58 using namespace ::com::sun::star::sdbc;
59 using namespace ::com::sun::star::container;
60 using namespace ::com::sun::star::lang;
61 typedef connectivity::sdbcx::OCollection OCollection_TYPE;
63 namespace connectivity
65 class OColumnsHelperImpl
67 public:
68 OColumnsHelperImpl(sal_Bool _bCase)
69 : m_aColumnInfo(_bCase)
72 ColumnInformationMap m_aColumnInfo;
76 OColumnsHelper::OColumnsHelper( ::cppu::OWeakObject& _rParent
77 ,sal_Bool _bCase
78 ,::osl::Mutex& _rMutex
79 ,const TStringVector &_rVector
80 ,sal_Bool _bUseHardRef
81 ) : OCollection(_rParent,_bCase,_rMutex,_rVector,sal_False,_bUseHardRef)
82 ,m_pImpl(NULL)
83 ,m_pTable(NULL)
86 // -----------------------------------------------------------------------------
87 OColumnsHelper::~OColumnsHelper()
89 delete m_pImpl;
90 m_pImpl = NULL;
92 // -----------------------------------------------------------------------------
94 sdbcx::ObjectType OColumnsHelper::createObject(const ::rtl::OUString& _rName)
96 OSL_ENSURE(m_pTable,"NO Table set. Error!");
97 Reference<XConnection> xConnection = m_pTable->getConnection();
99 if ( !m_pImpl )
100 m_pImpl = new OColumnsHelperImpl(isCaseSensitive());
102 sal_Bool bQueryInfo = sal_True;
103 sal_Bool bAutoIncrement = sal_False;
104 sal_Bool bIsCurrency = sal_False;
105 sal_Int32 nDataType = DataType::OTHER;
107 ColumnInformationMap::iterator aFind = m_pImpl->m_aColumnInfo.find(_rName);
108 if ( aFind == m_pImpl->m_aColumnInfo.end() ) // we have to fill it
110 ::rtl::OUString sComposedName = ::dbtools::composeTableNameForSelect( xConnection, m_pTable );
111 collectColumnInformation(xConnection,sComposedName,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("*")) ,m_pImpl->m_aColumnInfo);
112 aFind = m_pImpl->m_aColumnInfo.find(_rName);
114 if ( aFind != m_pImpl->m_aColumnInfo.end() )
116 bQueryInfo = sal_False;
117 bAutoIncrement = aFind->second.first.first;
118 bIsCurrency = aFind->second.first.second;
119 nDataType = aFind->second.second;
120 } // if ( aFind != m_pImpl->m_aColumnInfo.end() )
122 sdbcx::ObjectType xRet;
123 const ColumnDesc* pColDesc = m_pTable->getColumnDescription(_rName);
124 if ( pColDesc )
126 Reference<XPropertySet> xPr = m_pTable;
127 Reference<XKeysSupplier> xKeysSup(xPr,UNO_QUERY);
128 Reference<XNameAccess> xPrimaryKeyColumns;
129 if ( xKeysSup.is() )
131 const Reference<XIndexAccess> xKeys = xKeysSup->getKeys();
132 if ( xKeys.is() )
134 ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
135 const sal_Int32 nKeyCount = xKeys->getCount();
136 for(sal_Int32 nKeyIter = 0; nKeyIter < nKeyCount;++nKeyIter)
138 const Reference<XPropertySet> xKey(xKeys->getByIndex(nKeyIter),UNO_QUERY_THROW);
139 sal_Int32 nType = 0;
140 xKey->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_TYPE)) >>= nType;
141 if ( nType == KeyType::PRIMARY )
143 const Reference<XColumnsSupplier> xColS(xKey,UNO_QUERY_THROW);
144 xPrimaryKeyColumns = xColS->getColumns();
145 break;
147 } // for(sal_Int32 nKeyIter = 0; nKeyIter < nKeyCount;++)
150 sal_Int32 nField11 = pColDesc->nField11;
151 if ( nField11 != ColumnValue::NO_NULLS && xPrimaryKeyColumns.is() && xPrimaryKeyColumns->hasByName(_rName) )
153 nField11 = ColumnValue::NO_NULLS;
154 } // if ( xKeys.is() )
155 connectivity::sdbcx::OColumn* pRet = new connectivity::sdbcx::OColumn(_rName,
156 pColDesc->aField6,
157 pColDesc->sField13,
158 nField11,
159 pColDesc->nField7,
160 pColDesc->nField9,
161 pColDesc->nField5,
162 bAutoIncrement,
163 sal_False,
164 bIsCurrency,
165 isCaseSensitive());
167 xRet = pRet;
169 else
172 xRet.set(::dbtools::createSDBCXColumn( m_pTable,
173 xConnection,
174 _rName,
175 isCaseSensitive(),
176 bQueryInfo,
177 bAutoIncrement,
178 bIsCurrency,
179 nDataType),UNO_QUERY);
181 return xRet;
184 // -------------------------------------------------------------------------
185 void OColumnsHelper::impl_refresh() throw(RuntimeException)
187 if ( m_pTable )
189 m_pImpl->m_aColumnInfo.clear();
190 m_pTable->refreshColumns();
193 // -------------------------------------------------------------------------
194 Reference< XPropertySet > OColumnsHelper::createDescriptor()
196 return new OColumn(sal_True);
198 // -----------------------------------------------------------------------------
199 // XAppend
200 sdbcx::ObjectType OColumnsHelper::appendObject( const ::rtl::OUString& _rForName, const Reference< XPropertySet >& descriptor )
202 ::osl::MutexGuard aGuard(m_rMutex);
203 OSL_ENSURE(m_pTable,"OColumnsHelper::appendByDescriptor: Table is null!");
204 if ( !m_pTable || m_pTable->isNew() )
205 return cloneDescriptor( descriptor );
207 Reference<XDatabaseMetaData> xMetaData = m_pTable->getConnection()->getMetaData();
208 ::rtl::OUString aSql = ::rtl::OUString::createFromAscii("ALTER TABLE ");
209 ::rtl::OUString aQuote = xMetaData->getIdentifierQuoteString( );
211 aSql += ::dbtools::composeTableName( xMetaData, m_pTable, ::dbtools::eInTableDefinitions, false, false, true );
212 aSql += ::rtl::OUString::createFromAscii(" ADD ");
213 aSql += ::dbtools::createStandardColumnPart(descriptor,m_pTable->getConnection(),m_pTable->getTypeCreatePattern());
215 Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement( );
216 if ( xStmt.is() )
218 xStmt->execute(aSql);
219 ::comphelper::disposeComponent(xStmt);
221 return createObject( _rForName );
223 // -------------------------------------------------------------------------
224 // XDrop
225 void OColumnsHelper::dropObject(sal_Int32 /*_nPos*/,const ::rtl::OUString _sElementName)
227 OSL_ENSURE(m_pTable,"OColumnsHelper::dropByName: Table is null!");
228 if ( m_pTable && !m_pTable->isNew() )
230 ::rtl::OUString aSql = ::rtl::OUString::createFromAscii("ALTER TABLE ");
231 Reference<XDatabaseMetaData> xMetaData = m_pTable->getConnection()->getMetaData();
232 ::rtl::OUString aQuote = xMetaData->getIdentifierQuoteString( );
234 aSql += ::dbtools::composeTableName( xMetaData, m_pTable, ::dbtools::eInTableDefinitions, false, false, true );
235 aSql += ::rtl::OUString::createFromAscii(" DROP ");
236 aSql += ::dbtools::quoteName( aQuote,_sElementName);
238 Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement( );
239 if ( xStmt.is() )
241 xStmt->execute(aSql);
242 ::comphelper::disposeComponent(xStmt);
246 // -----------------------------------------------------------------------------