1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <connectivity/TColumnsHelper.hxx>
21 #include <connectivity/sdbcx/VColumn.hxx>
22 #include <com/sun/star/sdbc/DataType.hpp>
23 #include <com/sun/star/sdbc/ColumnValue.hpp>
24 #include <comphelper/types.hxx>
25 #include <connectivity/dbtools.hxx>
26 #include <TConnection.hxx>
27 #include <connectivity/TTableHelper.hxx>
29 using namespace ::comphelper
;
32 using namespace connectivity::sdbcx
;
33 using namespace connectivity
;
34 using namespace dbtools
;
35 using namespace ::com::sun::star::uno
;
36 using namespace ::com::sun::star::beans
;
37 using namespace ::com::sun::star::sdbc
;
38 using namespace ::com::sun::star::container
;
40 namespace connectivity
42 class OColumnsHelperImpl
45 explicit OColumnsHelperImpl(bool _bCase
)
46 : m_aColumnInfo(UStringMixLess(_bCase
))
49 ColumnInformationMap m_aColumnInfo
;
53 OColumnsHelper::OColumnsHelper( ::cppu::OWeakObject
& _rParent
55 ,::osl::Mutex
& _rMutex
56 ,const ::std::vector
< OUString
> &_rVector
58 ) : OCollection(_rParent
,_bCase
,_rMutex
,_rVector
,false,_bUseHardRef
)
63 OColumnsHelper::~OColumnsHelper()
67 sdbcx::ObjectType
OColumnsHelper::createObject(const OUString
& _rName
)
69 assert(m_pTable
&& "NO Table set. Error!");
70 Reference
<XConnection
> xConnection
= m_pTable
->getConnection();
73 m_pImpl
.reset(new OColumnsHelperImpl(isCaseSensitive()));
75 bool bQueryInfo
= true;
76 bool bAutoIncrement
= false;
77 bool bIsCurrency
= false;
78 sal_Int32 nDataType
= DataType::OTHER
;
80 ColumnInformationMap::const_iterator aFind
= m_pImpl
->m_aColumnInfo
.find(_rName
);
81 if ( aFind
== m_pImpl
->m_aColumnInfo
.end() ) // we have to fill it
83 OUString sComposedName
= ::dbtools::composeTableNameForSelect( xConnection
, m_pTable
);
84 collectColumnInformation(xConnection
,sComposedName
,u
"*" ,m_pImpl
->m_aColumnInfo
);
85 aFind
= m_pImpl
->m_aColumnInfo
.find(_rName
);
87 if ( aFind
!= m_pImpl
->m_aColumnInfo
.end() )
90 bAutoIncrement
= aFind
->second
.first
.first
;
91 bIsCurrency
= aFind
->second
.first
.second
;
92 nDataType
= aFind
->second
.second
;
93 } // if ( aFind != m_pImpl->m_aColumnInfo.end() )
95 sdbcx::ObjectType xRet
;
96 const ColumnDesc
* pColDesc
= m_pTable
->getColumnDescription(_rName
);
99 Reference
<XPropertySet
> xPr
= m_pTable
;
100 const Reference
<XNameAccess
> xPrimaryKeyColumns
= getPrimaryKeyColumns_throw(xPr
);
101 sal_Int32 nField11
= pColDesc
->nField11
;
102 if ( nField11
!= ColumnValue::NO_NULLS
&& xPrimaryKeyColumns
.is() && xPrimaryKeyColumns
->hasByName(_rName
) )
104 nField11
= ColumnValue::NO_NULLS
;
105 } // if ( xKeys.is() )
106 ::dbtools::OPropertyMap
& rPropMap
= OMetaConnection::getPropMap();
107 OUString aCatalog
, aSchema
, aTable
;
108 m_pTable
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_CATALOGNAME
)) >>= aCatalog
;
109 m_pTable
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_SCHEMANAME
)) >>= aSchema
;
110 m_pTable
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_NAME
)) >>= aTable
;
111 rtl::Reference
<connectivity::sdbcx::OColumn
> pRet
= new connectivity::sdbcx::OColumn(_rName
,
132 xRet
= ::dbtools::createSDBCXColumn( m_pTable
,
144 void OColumnsHelper::impl_refresh()
148 m_pImpl
->m_aColumnInfo
.clear();
149 m_pTable
->refreshColumns();
153 Reference
< XPropertySet
> OColumnsHelper::createDescriptor()
155 return new OColumn(true);
159 sdbcx::ObjectType
OColumnsHelper::appendObject( const OUString
& _rForName
, const Reference
< XPropertySet
>& descriptor
)
161 ::osl::MutexGuard
aGuard(m_rMutex
);
162 OSL_ENSURE(m_pTable
,"OColumnsHelper::appendByDescriptor: Table is null!");
163 if ( !m_pTable
|| m_pTable
->isNew() )
164 return cloneDescriptor( descriptor
);
166 Reference
<XDatabaseMetaData
> xMetaData
= m_pTable
->getConnection()->getMetaData();
167 OUString aSql
= "ALTER TABLE " +
168 ::dbtools::composeTableName( xMetaData
, m_pTable
, ::dbtools::EComposeRule::InTableDefinitions
, true ) +
170 ::dbtools::createStandardColumnPart(descriptor
,m_pTable
->getConnection(),nullptr,m_pTable
->getTypeCreatePattern());
172 Reference
< XStatement
> xStmt
= m_pTable
->getConnection()->createStatement( );
175 xStmt
->execute(aSql
);
176 ::comphelper::disposeComponent(xStmt
);
178 return createObject( _rForName
);
182 void OColumnsHelper::dropObject(sal_Int32
/*_nPos*/, const OUString
& _sElementName
)
184 OSL_ENSURE(m_pTable
,"OColumnsHelper::dropByName: Table is null!");
185 if ( !m_pTable
|| m_pTable
->isNew() )
188 Reference
<XDatabaseMetaData
> xMetaData
= m_pTable
->getConnection()->getMetaData();
189 OUString aQuote
= xMetaData
->getIdentifierQuoteString( );
190 OUString aSql
= "ALTER TABLE " +
191 ::dbtools::composeTableName( xMetaData
, m_pTable
, ::dbtools::EComposeRule::InTableDefinitions
, true ) +
193 ::dbtools::quoteName( aQuote
,_sElementName
);
195 Reference
< XStatement
> xStmt
= m_pTable
->getConnection()->createStatement( );
198 xStmt
->execute(aSql
);
199 ::comphelper::disposeComponent(xStmt
);
204 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */