bump product version to 6.3.0.0.beta1
[LibreOffice.git] / connectivity / source / commontools / TColumnsHelper.cxx
blob6155ec09443b40e38aff494a1be85354f56bc346
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
34 using namespace ::comphelper;
37 using namespace connectivity::sdbcx;
38 using namespace connectivity;
39 using namespace dbtools;
40 using namespace ::com::sun::star::uno;
41 using namespace ::com::sun::star::beans;
42 using namespace ::com::sun::star::sdbcx;
43 using namespace ::com::sun::star::sdbc;
44 using namespace ::com::sun::star::container;
45 using namespace ::com::sun::star::lang;
47 namespace connectivity
49 class OColumnsHelperImpl
51 public:
52 explicit OColumnsHelperImpl(bool _bCase)
53 : m_aColumnInfo(_bCase)
56 ColumnInformationMap m_aColumnInfo;
60 OColumnsHelper::OColumnsHelper( ::cppu::OWeakObject& _rParent
61 ,bool _bCase
62 ,::osl::Mutex& _rMutex
63 ,const ::std::vector< OUString> &_rVector
64 ,bool _bUseHardRef
65 ) : OCollection(_rParent,_bCase,_rMutex,_rVector,false,_bUseHardRef)
66 ,m_pTable(nullptr)
70 OColumnsHelper::~OColumnsHelper()
75 sdbcx::ObjectType OColumnsHelper::createObject(const OUString& _rName)
77 OSL_ENSURE(m_pTable,"NO Table set. Error!");
78 Reference<XConnection> xConnection = m_pTable->getConnection();
80 if ( !m_pImpl )
81 m_pImpl.reset(new OColumnsHelperImpl(isCaseSensitive()));
83 bool bQueryInfo = true;
84 bool bAutoIncrement = false;
85 bool bIsCurrency = false;
86 sal_Int32 nDataType = DataType::OTHER;
88 ColumnInformationMap::const_iterator aFind = m_pImpl->m_aColumnInfo.find(_rName);
89 if ( aFind == m_pImpl->m_aColumnInfo.end() ) // we have to fill it
91 OUString sComposedName = ::dbtools::composeTableNameForSelect( xConnection, m_pTable );
92 collectColumnInformation(xConnection,sComposedName,"*" ,m_pImpl->m_aColumnInfo);
93 aFind = m_pImpl->m_aColumnInfo.find(_rName);
95 if ( aFind != m_pImpl->m_aColumnInfo.end() )
97 bQueryInfo = false;
98 bAutoIncrement = aFind->second.first.first;
99 bIsCurrency = aFind->second.first.second;
100 nDataType = aFind->second.second;
101 } // if ( aFind != m_pImpl->m_aColumnInfo.end() )
103 sdbcx::ObjectType xRet;
104 const ColumnDesc* pColDesc = m_pTable->getColumnDescription(_rName);
105 if ( pColDesc )
107 Reference<XPropertySet> xPr = m_pTable;
108 const Reference<XNameAccess> xPrimaryKeyColumns = getPrimaryKeyColumns_throw(xPr);
109 sal_Int32 nField11 = pColDesc->nField11;
110 if ( nField11 != ColumnValue::NO_NULLS && xPrimaryKeyColumns.is() && xPrimaryKeyColumns->hasByName(_rName) )
112 nField11 = ColumnValue::NO_NULLS;
113 } // if ( xKeys.is() )
114 ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
115 OUString aCatalog, aSchema, aTable;
116 m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME)) >>= aCatalog;
117 m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_SCHEMANAME)) >>= aSchema;
118 m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)) >>= aTable;
119 connectivity::sdbcx::OColumn* pRet = new connectivity::sdbcx::OColumn(_rName,
120 pColDesc->aField6,
121 pColDesc->sField13,
122 pColDesc->sField12,
123 nField11,
124 pColDesc->nField7,
125 pColDesc->nField9,
126 pColDesc->nField5,
127 bAutoIncrement,
128 false,
129 bIsCurrency,
130 isCaseSensitive(),
131 aCatalog,
132 aSchema,
133 aTable);
135 xRet = pRet;
137 else
140 xRet.set(::dbtools::createSDBCXColumn( m_pTable,
141 xConnection,
142 _rName,
143 isCaseSensitive(),
144 bQueryInfo,
145 bAutoIncrement,
146 bIsCurrency,
147 nDataType),UNO_QUERY);
149 return xRet;
153 void OColumnsHelper::impl_refresh()
155 if ( m_pTable )
157 m_pImpl->m_aColumnInfo.clear();
158 m_pTable->refreshColumns();
162 Reference< XPropertySet > OColumnsHelper::createDescriptor()
164 return new OColumn(true);
167 // XAppend
168 sdbcx::ObjectType OColumnsHelper::appendObject( const OUString& _rForName, const Reference< XPropertySet >& descriptor )
170 ::osl::MutexGuard aGuard(m_rMutex);
171 OSL_ENSURE(m_pTable,"OColumnsHelper::appendByDescriptor: Table is null!");
172 if ( !m_pTable || m_pTable->isNew() )
173 return cloneDescriptor( descriptor );
175 Reference<XDatabaseMetaData> xMetaData = m_pTable->getConnection()->getMetaData();
176 OUString aSql = "ALTER TABLE " +
177 ::dbtools::composeTableName( xMetaData, m_pTable, ::dbtools::EComposeRule::InTableDefinitions, true ) +
178 " ADD " +
179 ::dbtools::createStandardColumnPart(descriptor,m_pTable->getConnection(),nullptr,m_pTable->getTypeCreatePattern());
181 Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement( );
182 if ( xStmt.is() )
184 xStmt->execute(aSql);
185 ::comphelper::disposeComponent(xStmt);
187 return createObject( _rForName );
190 // XDrop
191 void OColumnsHelper::dropObject(sal_Int32 /*_nPos*/, const OUString& _sElementName)
193 OSL_ENSURE(m_pTable,"OColumnsHelper::dropByName: Table is null!");
194 if ( m_pTable && !m_pTable->isNew() )
196 Reference<XDatabaseMetaData> xMetaData = m_pTable->getConnection()->getMetaData();
197 OUString aQuote = xMetaData->getIdentifierQuoteString( );
198 OUString aSql = "ALTER TABLE " +
199 ::dbtools::composeTableName( xMetaData, m_pTable, ::dbtools::EComposeRule::InTableDefinitions, true ) +
200 " DROP " +
201 ::dbtools::quoteName( aQuote,_sElementName);
203 Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement( );
204 if ( xStmt.is() )
206 xStmt->execute(aSql);
207 ::comphelper::disposeComponent(xStmt);
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */