nss: upgrade to release 3.73
[LibreOffice.git] / connectivity / source / commontools / TColumnsHelper.cxx
blobf06393778953022423f409835bc39c6999a23bc2
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/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
46 public:
47 explicit OColumnsHelperImpl(bool _bCase)
48 : m_aColumnInfo(_bCase)
51 ColumnInformationMap m_aColumnInfo;
55 OColumnsHelper::OColumnsHelper( ::cppu::OWeakObject& _rParent
56 ,bool _bCase
57 ,::osl::Mutex& _rMutex
58 ,const ::std::vector< OUString> &_rVector
59 ,bool _bUseHardRef
60 ) : OCollection(_rParent,_bCase,_rMutex,_rVector,false,_bUseHardRef)
61 ,m_pTable(nullptr)
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();
75 if ( !m_pImpl )
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() )
92 bQueryInfo = false;
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);
100 if ( pColDesc )
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,
115 pColDesc->aField6,
116 pColDesc->sField13,
117 pColDesc->sField12,
118 nField11,
119 pColDesc->nField7,
120 pColDesc->nField9,
121 pColDesc->nField5,
122 bAutoIncrement,
123 false,
124 bIsCurrency,
125 isCaseSensitive(),
126 aCatalog,
127 aSchema,
128 aTable);
130 xRet = pRet;
132 else
135 xRet = ::dbtools::createSDBCXColumn( m_pTable,
136 xConnection,
137 _rName,
138 isCaseSensitive(),
139 bQueryInfo,
140 bAutoIncrement,
141 bIsCurrency,
142 nDataType);
144 return xRet;
148 void OColumnsHelper::impl_refresh()
150 if ( m_pTable )
152 m_pImpl->m_aColumnInfo.clear();
153 m_pTable->refreshColumns();
157 Reference< XPropertySet > OColumnsHelper::createDescriptor()
159 return new OColumn(true);
162 // XAppend
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 ) +
173 " ADD " +
174 ::dbtools::createStandardColumnPart(descriptor,m_pTable->getConnection(),nullptr,m_pTable->getTypeCreatePattern());
176 Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement( );
177 if ( xStmt.is() )
179 xStmt->execute(aSql);
180 ::comphelper::disposeComponent(xStmt);
182 return createObject( _rForName );
185 // XDrop
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() )
190 return;
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 ) +
196 " DROP " +
197 ::dbtools::quoteName( aQuote,_sElementName);
199 Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement( );
200 if ( xStmt.is() )
202 xStmt->execute(aSql);
203 ::comphelper::disposeComponent(xStmt);
208 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */