/src/libreoffice/connectivity/source/commontools/TColumnsHelper.cxx
Line | Count | Source |
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 | | |
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> |
28 | | |
29 | | using namespace ::comphelper; |
30 | | |
31 | | |
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::sdbc; |
38 | | using namespace ::com::sun::star::container; |
39 | | |
40 | | namespace connectivity |
41 | | { |
42 | | class OColumnsHelperImpl |
43 | | { |
44 | | public: |
45 | | explicit OColumnsHelperImpl(bool _bCase) |
46 | 9.55k | : m_aColumnInfo(UStringMixLess(_bCase)) |
47 | 9.55k | { |
48 | 9.55k | } |
49 | | ColumnInformationMap m_aColumnInfo; |
50 | | }; |
51 | | } |
52 | | |
53 | | OColumnsHelper::OColumnsHelper( ::cppu::OWeakObject& _rParent |
54 | | ,bool _bCase |
55 | | ,::osl::Mutex& _rMutex |
56 | | ,const ::std::vector< OUString> &_rVector |
57 | | ,bool _bUseHardRef |
58 | 9.59k | ) : OCollection(_rParent,_bCase,_rMutex,_rVector,false,_bUseHardRef) |
59 | 9.59k | ,m_pTable(nullptr) |
60 | 9.59k | { |
61 | 9.59k | } |
62 | | |
63 | | OColumnsHelper::~OColumnsHelper() |
64 | 9.59k | { |
65 | 9.59k | } |
66 | | |
67 | | css::uno::Reference< css::beans::XPropertySet > OColumnsHelper::createObject(const OUString& _rName) |
68 | 144k | { |
69 | 144k | assert(m_pTable && "NO Table set. Error!"); |
70 | 144k | Reference<XConnection> xConnection = m_pTable->getConnection(); |
71 | | |
72 | 144k | if ( !m_pImpl ) |
73 | 9.55k | m_pImpl.reset(new OColumnsHelperImpl(isCaseSensitive())); |
74 | | |
75 | 144k | bool bQueryInfo = true; |
76 | 144k | bool bAutoIncrement = false; |
77 | 144k | bool bIsCurrency = false; |
78 | 144k | sal_Int32 nDataType = DataType::OTHER; |
79 | | |
80 | 144k | ColumnInformationMap::const_iterator aFind = m_pImpl->m_aColumnInfo.find(_rName); |
81 | 144k | if ( aFind == m_pImpl->m_aColumnInfo.end() ) // we have to fill it |
82 | 9.55k | { |
83 | 9.55k | OUString sComposedName = ::dbtools::composeTableNameForSelect( xConnection, m_pTable ); |
84 | 9.55k | collectColumnInformation(xConnection,sComposedName,u"*" ,m_pImpl->m_aColumnInfo); |
85 | 9.55k | aFind = m_pImpl->m_aColumnInfo.find(_rName); |
86 | 9.55k | } |
87 | 144k | if ( aFind != m_pImpl->m_aColumnInfo.end() ) |
88 | 144k | { |
89 | 144k | bQueryInfo = false; |
90 | 144k | bAutoIncrement = aFind->second.first.first; |
91 | 144k | bIsCurrency = aFind->second.first.second; |
92 | 144k | nDataType = aFind->second.second; |
93 | 144k | } // if ( aFind != m_pImpl->m_aColumnInfo.end() ) |
94 | | |
95 | 144k | css::uno::Reference< css::beans::XPropertySet > xRet; |
96 | 144k | const ColumnDesc* pColDesc = m_pTable->getColumnDescription(_rName); |
97 | 144k | if ( pColDesc ) |
98 | 144k | { |
99 | 144k | Reference<XPropertySet> xPr = m_pTable; |
100 | 144k | const Reference<XNameAccess> xPrimaryKeyColumns = getPrimaryKeyColumns_throw(xPr); |
101 | 144k | sal_Int32 nField11 = pColDesc->nField11; |
102 | 144k | if ( nField11 != ColumnValue::NO_NULLS && xPrimaryKeyColumns.is() && xPrimaryKeyColumns->hasByName(_rName) ) |
103 | 0 | { |
104 | 0 | nField11 = ColumnValue::NO_NULLS; |
105 | 0 | } // if ( xKeys.is() ) |
106 | 144k | ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap(); |
107 | 144k | OUString aCatalog, aSchema, aTable; |
108 | 144k | m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME)) >>= aCatalog; |
109 | 144k | m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_SCHEMANAME)) >>= aSchema; |
110 | 144k | m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)) >>= aTable; |
111 | 144k | rtl::Reference<connectivity::sdbcx::OColumn> pRet = new connectivity::sdbcx::OColumn(_rName, |
112 | 144k | pColDesc->aField6, |
113 | 144k | pColDesc->sField13, |
114 | 144k | pColDesc->sField12, |
115 | 144k | nField11, |
116 | 144k | pColDesc->nField7, |
117 | 144k | pColDesc->nField9, |
118 | 144k | pColDesc->nField5, |
119 | 144k | bAutoIncrement, |
120 | 144k | false, |
121 | 144k | bIsCurrency, |
122 | 144k | isCaseSensitive(), |
123 | 144k | aCatalog, |
124 | 144k | aSchema, |
125 | 144k | aTable); |
126 | | |
127 | 144k | xRet = pRet; |
128 | 144k | } |
129 | 0 | else |
130 | 0 | { |
131 | |
|
132 | 0 | xRet = ::dbtools::createSDBCXColumn( m_pTable, |
133 | 0 | xConnection, |
134 | 0 | _rName, |
135 | 0 | isCaseSensitive(), |
136 | 0 | bQueryInfo, |
137 | 0 | bAutoIncrement, |
138 | 0 | bIsCurrency, |
139 | 0 | nDataType); |
140 | 0 | } |
141 | 144k | return xRet; |
142 | 144k | } |
143 | | |
144 | | void OColumnsHelper::impl_refresh() |
145 | 0 | { |
146 | 0 | if ( m_pTable ) |
147 | 0 | { |
148 | 0 | m_pImpl->m_aColumnInfo.clear(); |
149 | 0 | m_pTable->refreshColumns(); |
150 | 0 | } |
151 | 0 | } |
152 | | |
153 | | Reference< XPropertySet > OColumnsHelper::createDescriptor() |
154 | 0 | { |
155 | 0 | return new OColumn(true); |
156 | 0 | } |
157 | | |
158 | | // XAppend |
159 | | css::uno::Reference< css::beans::XPropertySet > OColumnsHelper::appendObject( const OUString& _rForName, const Reference< XPropertySet >& descriptor ) |
160 | 0 | { |
161 | 0 | ::osl::MutexGuard aGuard(m_rMutex); |
162 | 0 | OSL_ENSURE(m_pTable,"OColumnsHelper::appendByDescriptor: Table is null!"); |
163 | 0 | if ( !m_pTable || m_pTable->isNew() ) |
164 | 0 | return cloneDescriptor( descriptor ); |
165 | | |
166 | 0 | Reference<XDatabaseMetaData> xMetaData = m_pTable->getConnection()->getMetaData(); |
167 | 0 | OUString aSql = "ALTER TABLE " + |
168 | 0 | ::dbtools::composeTableName( xMetaData, m_pTable, ::dbtools::EComposeRule::InTableDefinitions, true ) + |
169 | 0 | " ADD " + |
170 | 0 | ::dbtools::createStandardColumnPart(descriptor,m_pTable->getConnection(),nullptr,m_pTable->getTypeCreatePattern()); |
171 | |
|
172 | 0 | Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement( ); |
173 | 0 | if ( xStmt.is() ) |
174 | 0 | { |
175 | 0 | xStmt->execute(aSql); |
176 | 0 | ::comphelper::disposeComponent(xStmt); |
177 | 0 | } |
178 | 0 | return createObject( _rForName ); |
179 | 0 | } |
180 | | |
181 | | // XDrop |
182 | | void OColumnsHelper::dropObject(sal_Int32 /*_nPos*/, const OUString& _sElementName) |
183 | 0 | { |
184 | 0 | OSL_ENSURE(m_pTable,"OColumnsHelper::dropByName: Table is null!"); |
185 | 0 | if ( !m_pTable || m_pTable->isNew() ) |
186 | 0 | return; |
187 | | |
188 | 0 | Reference<XDatabaseMetaData> xMetaData = m_pTable->getConnection()->getMetaData(); |
189 | 0 | OUString aQuote = xMetaData->getIdentifierQuoteString( ); |
190 | 0 | OUString aSql = "ALTER TABLE " + |
191 | 0 | ::dbtools::composeTableName( xMetaData, m_pTable, ::dbtools::EComposeRule::InTableDefinitions, true ) + |
192 | 0 | " DROP " + |
193 | 0 | ::dbtools::quoteName( aQuote,_sElementName); |
194 | |
|
195 | 0 | Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement( ); |
196 | 0 | if ( xStmt.is() ) |
197 | 0 | { |
198 | 0 | xStmt->execute(aSql); |
199 | 0 | ::comphelper::disposeComponent(xStmt); |
200 | 0 | } |
201 | 0 | } |
202 | | |
203 | | |
204 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |