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::sdbcx
;
38 using namespace ::com::sun::star::sdbc
;
39 using namespace ::com::sun::star::container
;
40 using namespace ::com::sun::star::lang
;
42 namespace connectivity
44 class OColumnsHelperImpl
47 explicit OColumnsHelperImpl(bool _bCase
)
48 : m_aColumnInfo(_bCase
)
51 ColumnInformationMap m_aColumnInfo
;
55 OColumnsHelper::OColumnsHelper( ::cppu::OWeakObject
& _rParent
57 ,::osl::Mutex
& _rMutex
58 ,const ::std::vector
< OUString
> &_rVector
60 ) : OCollection(_rParent
,_bCase
,_rMutex
,_rVector
,false,_bUseHardRef
)
65 OColumnsHelper::~OColumnsHelper()
70 sdbcx::ObjectType
OColumnsHelper::createObject(const OUString
& _rName
)
72 OSL_ENSURE(m_pTable
,"NO Table set. Error!");
73 Reference
<XConnection
> xConnection
= m_pTable
->getConnection();
76 m_pImpl
.reset(new OColumnsHelperImpl(isCaseSensitive()));
78 bool bQueryInfo
= true;
79 bool bAutoIncrement
= false;
80 bool bIsCurrency
= false;
81 sal_Int32 nDataType
= DataType::OTHER
;
83 ColumnInformationMap::const_iterator aFind
= m_pImpl
->m_aColumnInfo
.find(_rName
);
84 if ( aFind
== m_pImpl
->m_aColumnInfo
.end() ) // we have to fill it
86 OUString sComposedName
= ::dbtools::composeTableNameForSelect( xConnection
, m_pTable
);
87 collectColumnInformation(xConnection
,sComposedName
,"*" ,m_pImpl
->m_aColumnInfo
);
88 aFind
= m_pImpl
->m_aColumnInfo
.find(_rName
);
90 if ( aFind
!= m_pImpl
->m_aColumnInfo
.end() )
93 bAutoIncrement
= aFind
->second
.first
.first
;
94 bIsCurrency
= aFind
->second
.first
.second
;
95 nDataType
= aFind
->second
.second
;
96 } // if ( aFind != m_pImpl->m_aColumnInfo.end() )
98 sdbcx::ObjectType xRet
;
99 const ColumnDesc
* pColDesc
= m_pTable
->getColumnDescription(_rName
);
102 Reference
<XPropertySet
> xPr
= m_pTable
;
103 const Reference
<XNameAccess
> xPrimaryKeyColumns
= getPrimaryKeyColumns_throw(xPr
);
104 sal_Int32 nField11
= pColDesc
->nField11
;
105 if ( nField11
!= ColumnValue::NO_NULLS
&& xPrimaryKeyColumns
.is() && xPrimaryKeyColumns
->hasByName(_rName
) )
107 nField11
= ColumnValue::NO_NULLS
;
108 } // if ( xKeys.is() )
109 ::dbtools::OPropertyMap
& rPropMap
= OMetaConnection::getPropMap();
110 OUString aCatalog
, aSchema
, aTable
;
111 m_pTable
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_CATALOGNAME
)) >>= aCatalog
;
112 m_pTable
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_SCHEMANAME
)) >>= aSchema
;
113 m_pTable
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_NAME
)) >>= aTable
;
114 connectivity::sdbcx::OColumn
* pRet
= new connectivity::sdbcx::OColumn(_rName
,
135 xRet
= ::dbtools::createSDBCXColumn( m_pTable
,
148 void OColumnsHelper::impl_refresh()
152 m_pImpl
->m_aColumnInfo
.clear();
153 m_pTable
->refreshColumns();
157 Reference
< XPropertySet
> OColumnsHelper::createDescriptor()
159 return new OColumn(true);
163 sdbcx::ObjectType
OColumnsHelper::appendObject( const OUString
& _rForName
, const Reference
< XPropertySet
>& descriptor
)
165 ::osl::MutexGuard
aGuard(m_rMutex
);
166 OSL_ENSURE(m_pTable
,"OColumnsHelper::appendByDescriptor: Table is null!");
167 if ( !m_pTable
|| m_pTable
->isNew() )
168 return cloneDescriptor( descriptor
);
170 Reference
<XDatabaseMetaData
> xMetaData
= m_pTable
->getConnection()->getMetaData();
171 OUString aSql
= "ALTER TABLE " +
172 ::dbtools::composeTableName( xMetaData
, m_pTable
, ::dbtools::EComposeRule::InTableDefinitions
, true ) +
174 ::dbtools::createStandardColumnPart(descriptor
,m_pTable
->getConnection(),nullptr,m_pTable
->getTypeCreatePattern());
176 Reference
< XStatement
> xStmt
= m_pTable
->getConnection()->createStatement( );
179 xStmt
->execute(aSql
);
180 ::comphelper::disposeComponent(xStmt
);
182 return createObject( _rForName
);
186 void OColumnsHelper::dropObject(sal_Int32
/*_nPos*/, const OUString
& _sElementName
)
188 OSL_ENSURE(m_pTable
,"OColumnsHelper::dropByName: Table is null!");
189 if ( !m_pTable
|| m_pTable
->isNew() )
192 Reference
<XDatabaseMetaData
> xMetaData
= m_pTable
->getConnection()->getMetaData();
193 OUString aQuote
= xMetaData
->getIdentifierQuoteString( );
194 OUString aSql
= "ALTER TABLE " +
195 ::dbtools::composeTableName( xMetaData
, m_pTable
, ::dbtools::EComposeRule::InTableDefinitions
, true ) +
197 ::dbtools::quoteName( aQuote
,_sElementName
);
199 Reference
< XStatement
> xStmt
= m_pTable
->getConnection()->createStatement( );
202 xStmt
->execute(aSql
);
203 ::comphelper::disposeComponent(xStmt
);
208 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */