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/XRow.hpp>
23 #include <com/sun/star/sdbc/XResultSet.hpp>
24 #include <com/sun/star/sdbc/DataType.hpp>
25 #include <com/sun/star/sdbc/ColumnValue.hpp>
26 #include <com/sun/star/sdbcx/KeyType.hpp>
27 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
28 #include <com/sun/star/sdbcx/XKeysSupplier.hpp>
29 #include <comphelper/types.hxx>
30 #include <connectivity/dbtools.hxx>
31 #include "TConnection.hxx"
32 #include <connectivity/TTableHelper.hxx>
33 #include <comphelper/property.hxx>
35 using namespace ::comphelper
;
38 using namespace connectivity::sdbcx
;
39 using namespace connectivity
;
40 using namespace dbtools
;
41 using namespace ::com::sun::star::uno
;
42 using namespace ::com::sun::star::beans
;
43 using namespace ::com::sun::star::sdbcx
;
44 using namespace ::com::sun::star::sdbc
;
45 using namespace ::com::sun::star::container
;
46 using namespace ::com::sun::star::lang
;
48 namespace connectivity
50 class OColumnsHelperImpl
53 OColumnsHelperImpl(bool _bCase
)
54 : m_aColumnInfo(_bCase
)
57 ColumnInformationMap m_aColumnInfo
;
61 OColumnsHelper::OColumnsHelper( ::cppu::OWeakObject
& _rParent
63 ,::osl::Mutex
& _rMutex
64 ,const TStringVector
&_rVector
66 ) : OCollection(_rParent
,_bCase
,_rMutex
,_rVector
,false,_bUseHardRef
)
72 OColumnsHelper::~OColumnsHelper()
79 sdbcx::ObjectType
OColumnsHelper::createObject(const OUString
& _rName
)
81 OSL_ENSURE(m_pTable
,"NO Table set. Error!");
82 Reference
<XConnection
> xConnection
= m_pTable
->getConnection();
85 m_pImpl
= new OColumnsHelperImpl(isCaseSensitive());
87 bool bQueryInfo
= true;
88 bool bAutoIncrement
= false;
89 bool bIsCurrency
= false;
90 sal_Int32 nDataType
= DataType::OTHER
;
92 ColumnInformationMap::iterator aFind
= m_pImpl
->m_aColumnInfo
.find(_rName
);
93 if ( aFind
== m_pImpl
->m_aColumnInfo
.end() ) // we have to fill it
95 OUString sComposedName
= ::dbtools::composeTableNameForSelect( xConnection
, m_pTable
);
96 collectColumnInformation(xConnection
,sComposedName
,OUString("*") ,m_pImpl
->m_aColumnInfo
);
97 aFind
= m_pImpl
->m_aColumnInfo
.find(_rName
);
99 if ( aFind
!= m_pImpl
->m_aColumnInfo
.end() )
102 bAutoIncrement
= aFind
->second
.first
.first
;
103 bIsCurrency
= aFind
->second
.first
.second
;
104 nDataType
= aFind
->second
.second
;
105 } // if ( aFind != m_pImpl->m_aColumnInfo.end() )
107 sdbcx::ObjectType xRet
;
108 const ColumnDesc
* pColDesc
= m_pTable
->getColumnDescription(_rName
);
111 Reference
<XPropertySet
> xPr
= m_pTable
;
112 const Reference
<XNameAccess
> xPrimaryKeyColumns
= getPrimaryKeyColumns_throw(xPr
);
113 sal_Int32 nField11
= pColDesc
->nField11
;
114 if ( nField11
!= ColumnValue::NO_NULLS
&& xPrimaryKeyColumns
.is() && xPrimaryKeyColumns
->hasByName(_rName
) )
116 nField11
= ColumnValue::NO_NULLS
;
117 } // if ( xKeys.is() )
118 ::dbtools::OPropertyMap
& rPropMap
= OMetaConnection::getPropMap();
119 OUString aCatalog
, aSchema
, aTable
;
120 m_pTable
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_CATALOGNAME
)) >>= aCatalog
;
121 m_pTable
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_SCHEMANAME
)) >>= aSchema
;
122 m_pTable
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_NAME
)) >>= aTable
;
123 connectivity::sdbcx::OColumn
* pRet
= new connectivity::sdbcx::OColumn(_rName
,
144 xRet
.set(::dbtools::createSDBCXColumn( m_pTable
,
151 nDataType
),UNO_QUERY
);
157 void OColumnsHelper::impl_refresh() throw(RuntimeException
)
161 m_pImpl
->m_aColumnInfo
.clear();
162 m_pTable
->refreshColumns();
166 Reference
< XPropertySet
> OColumnsHelper::createDescriptor()
168 return new OColumn(true);
172 sdbcx::ObjectType
OColumnsHelper::appendObject( const OUString
& _rForName
, const Reference
< XPropertySet
>& descriptor
)
174 ::osl::MutexGuard
aGuard(m_rMutex
);
175 OSL_ENSURE(m_pTable
,"OColumnsHelper::appendByDescriptor: Table is null!");
176 if ( !m_pTable
|| m_pTable
->isNew() )
177 return cloneDescriptor( descriptor
);
179 Reference
<XDatabaseMetaData
> xMetaData
= m_pTable
->getConnection()->getMetaData();
180 OUString aSql
= "ALTER TABLE " +
181 ::dbtools::composeTableName( xMetaData
, m_pTable
, ::dbtools::eInTableDefinitions
, false, false, true ) +
183 ::dbtools::createStandardColumnPart(descriptor
,m_pTable
->getConnection(),NULL
,m_pTable
->getTypeCreatePattern());
185 Reference
< XStatement
> xStmt
= m_pTable
->getConnection()->createStatement( );
188 xStmt
->execute(aSql
);
189 ::comphelper::disposeComponent(xStmt
);
191 return createObject( _rForName
);
195 void OColumnsHelper::dropObject(sal_Int32
/*_nPos*/, const OUString
& _sElementName
)
197 OSL_ENSURE(m_pTable
,"OColumnsHelper::dropByName: Table is null!");
198 if ( m_pTable
&& !m_pTable
->isNew() )
200 Reference
<XDatabaseMetaData
> xMetaData
= m_pTable
->getConnection()->getMetaData();
201 OUString aQuote
= xMetaData
->getIdentifierQuoteString( );
202 OUString aSql
= "ALTER TABLE " +
203 ::dbtools::composeTableName( xMetaData
, m_pTable
, ::dbtools::eInTableDefinitions
, false, false, true ) +
205 ::dbtools::quoteName( aQuote
,_sElementName
);
207 Reference
< XStatement
> xStmt
= m_pTable
->getConnection()->createStatement( );
210 xStmt
->execute(aSql
);
211 ::comphelper::disposeComponent(xStmt
);
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */