LibreOffice Module connectivity (master) 1
TColumnsHelper.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
22#include <com/sun/star/sdbc/DataType.hpp>
23#include <com/sun/star/sdbc/ColumnValue.hpp>
24#include <comphelper/types.hxx>
26#include <TConnection.hxx>
28
29using namespace ::comphelper;
30
31
32using namespace connectivity::sdbcx;
33using namespace connectivity;
34using namespace dbtools;
35using namespace ::com::sun::star::uno;
36using namespace ::com::sun::star::beans;
37using namespace ::com::sun::star::sdbcx;
38using namespace ::com::sun::star::sdbc;
39using namespace ::com::sun::star::container;
40using namespace ::com::sun::star::lang;
41
42namespace connectivity
43{
45 {
46 public:
47 explicit OColumnsHelperImpl(bool _bCase)
48 : m_aColumnInfo(_bCase)
49 {
50 }
52 };
53}
54
55OColumnsHelper::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)
62{
63}
64
66{
67}
68
69
71{
72 OSL_ENSURE(m_pTable,"NO Table set. Error!");
73 Reference<XConnection> xConnection = m_pTable->getConnection();
74
75 if ( !m_pImpl )
77
78 bool bQueryInfo = true;
79 bool bAutoIncrement = false;
80 bool bIsCurrency = false;
81 sal_Int32 nDataType = DataType::OTHER;
82
83 ColumnInformationMap::const_iterator aFind = m_pImpl->m_aColumnInfo.find(_rName);
84 if ( aFind == m_pImpl->m_aColumnInfo.end() ) // we have to fill it
85 {
87 collectColumnInformation(xConnection,sComposedName,u"*" ,m_pImpl->m_aColumnInfo);
88 aFind = m_pImpl->m_aColumnInfo.find(_rName);
89 }
90 if ( aFind != m_pImpl->m_aColumnInfo.end() )
91 {
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() )
97
99 const ColumnDesc* pColDesc = m_pTable->getColumnDescription(_rName);
100 if ( pColDesc )
101 {
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) )
106 {
107 nField11 = ColumnValue::NO_NULLS;
108 } // if ( xKeys.is() )
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;
115 pColDesc->aField6,
116 pColDesc->sField13,
117 pColDesc->sField12,
118 nField11,
119 pColDesc->nField7,
120 pColDesc->nField9,
121 pColDesc->nField5,
123 false,
124 bIsCurrency,
126 aCatalog,
127 aSchema,
128 aTable);
129
130 xRet = pRet;
131 }
132 else
133 {
134
136 xConnection,
137 _rName,
139 bQueryInfo,
141 bIsCurrency,
142 nDataType);
143 }
144 return xRet;
145}
146
147
149{
150 if ( m_pTable )
151 {
152 m_pImpl->m_aColumnInfo.clear();
154 }
155}
156
157Reference< XPropertySet > OColumnsHelper::createDescriptor()
158{
159 return new OColumn(true);
160}
161
162// XAppend
163sdbcx::ObjectType OColumnsHelper::appendObject( const OUString& _rForName, const Reference< XPropertySet >& descriptor )
164{
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 );
169
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 " +
175
176 Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement( );
177 if ( xStmt.is() )
178 {
179 xStmt->execute(aSql);
180 ::comphelper::disposeComponent(xStmt);
181 }
182 return createObject( _rForName );
183}
184
185// XDrop
186void OColumnsHelper::dropObject(sal_Int32 /*_nPos*/, const OUString& _sElementName)
187{
188 OSL_ENSURE(m_pTable,"OColumnsHelper::dropByName: Table is null!");
189 if ( !m_pTable || m_pTable->isNew() )
190 return;
191
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);
198
199 Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement( );
200 if ( xStmt.is() )
201 {
202 xStmt->execute(aSql);
203 ::comphelper::disposeComponent(xStmt);
204 }
205}
206
207
208/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 nDataType
OptionalString sComposedName
ColumnInformationMap m_aColumnInfo
virtual ~OColumnsHelper() override
virtual sdbcx::ObjectType createObject(const OUString &_rName) override
virtual void impl_refresh() override
virtual sdbcx::ObjectType appendObject(const OUString &_rForName, const css::uno::Reference< css::beans::XPropertySet > &descriptor) override
appends an object described by a descriptor, under a given name
virtual void dropObject(sal_Int32 _nPos, const OUString &_sElementName) override
virtual css::uno::Reference< css::beans::XPropertySet > createDescriptor() override
std::unique_ptr< OColumnsHelperImpl > m_pImpl
::dbtools::OPropertyMap & getPropMap()
Definition: TConnection.cxx:68
css::uno::Reference< css::sdbc::XConnection > const & getConnection() const
const ColumnDesc * getColumnDescription(const OUString &_sName) const
virtual OUString getTypeCreatePattern() const
virtual void refreshColumns() override
ObjectType cloneDescriptor(const ObjectType &_descriptor)
clones the given descriptor
const OUString & getNameByIndex(sal_Int32 _nIndex) const
Definition: propertyids.cxx:95
float u
bool bAutoIncrement
css::uno::Reference< css::beans::XPropertySet > ObjectType
Definition: VCollection.hxx:59
OUString composeTableName(const Reference< XDatabaseMetaData > &_rxMetaData, const OUString &_rCatalog, const OUString &_rSchema, const OUString &_rName, bool _bQuote, EComposeRule _eComposeRule)
Definition: dbtools.cxx:1286
OUString createStandardColumnPart(const Reference< XPropertySet > &xColProp, const Reference< XConnection > &_xConnection, ISQLStatementHelper *_pHelper, std::u16string_view _sCreatePattern)
Definition: dbtools2.cxx:169
Reference< XPropertySet > createSDBCXColumn(const Reference< XPropertySet > &_xTable, const Reference< XConnection > &_xConnection, const OUString &_rName, bool _bCase, bool _bQueryForInfo, bool _bIsAutoIncrement, bool _bIsCurrency, sal_Int32 _nDataType)
Definition: dbtools2.cxx:516
OUString composeTableNameForSelect(const Reference< XConnection > &_rxConnection, const OUString &_rCatalog, const OUString &_rSchema, const OUString &_rName)
Definition: dbtools.cxx:1296
::std::multimap< OUString, ColumnInformation, ::comphelper::UStringMixLess > ColumnInformationMap
Definition: dbtools.hxx:745
OUString quoteName(std::u16string_view _rQuote, const OUString &_rName)
quote the given name with the given quote string.
void collectColumnInformation(const Reference< XConnection > &_xConnection, std::u16string_view _sComposedName, std::u16string_view _rName, ColumnInformationMap &_rInfo)
Definition: dbtools2.cxx:795
Reference< XNameAccess > getPrimaryKeyColumns_throw(const Any &i_aTable)
Definition: dbtools.cxx:487
#define PROPERTY_ID_NAME
Definition: propertyids.hxx:50
#define PROPERTY_ID_CATALOGNAME
Definition: propertyids.hxx:70
#define PROPERTY_ID_SCHEMANAME
Definition: propertyids.hxx:69