Line data Source code
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 <string.h>
21 :
22 : #include "table.hxx"
23 : #include <definitioncolumn.hxx>
24 : #include "dbastrings.hrc"
25 : #include "core_resource.hxx"
26 : #include "core_resource.hrc"
27 : #include "CIndexes.hxx"
28 :
29 : #include <tools/debug.hxx>
30 : #include <osl/diagnose.h>
31 : #include <cppuhelper/typeprovider.hxx>
32 : #include <comphelper/enumhelper.hxx>
33 : #include <comphelper/container.hxx>
34 : #include <comphelper/sequence.hxx>
35 : #include <comphelper/types.hxx>
36 : #include <com/sun/star/util/XRefreshListener.hpp>
37 : #include <com/sun/star/sdbc/XConnection.hpp>
38 : #include <com/sun/star/sdbc/XRow.hpp>
39 : #include <com/sun/star/sdbcx/Privilege.hpp>
40 : #include <com/sun/star/sdbc/XResultSetMetaData.hpp>
41 : #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
42 :
43 : #include <connectivity/TKeys.hxx>
44 : #include <connectivity/dbtools.hxx>
45 : #include <connectivity/dbexception.hxx>
46 :
47 : #include "sdbcoretools.hxx"
48 : #include "ContainerMediator.hxx"
49 :
50 : using namespace dbaccess;
51 : using namespace connectivity;
52 : using namespace ::com::sun::star::uno;
53 : using namespace ::com::sun::star::util;
54 : using namespace ::com::sun::star::lang;
55 : using namespace ::com::sun::star::beans;
56 : using namespace ::com::sun::star::sdbc;
57 : using namespace ::com::sun::star::sdbcx;
58 : using namespace ::com::sun::star::container;
59 : using namespace ::osl;
60 : using namespace ::comphelper;
61 : using namespace ::cppu;
62 :
63 : // ODBTable
64 :
65 0 : ODBTable::ODBTable(connectivity::sdbcx::OCollection* _pTables
66 : ,const Reference< XConnection >& _rxConn
67 : ,const OUString& _rCatalog
68 : ,const OUString& _rSchema
69 : ,const OUString& _rName
70 : ,const OUString& _rType
71 : ,const OUString& _rDesc
72 : ,const Reference< XNameAccess >& _xColumnDefinitions) throw(SQLException)
73 0 : :OTable_Base(_pTables,_rxConn,_rxConn->getMetaData().is() && _rxConn->getMetaData()->supportsMixedCaseQuotedIdentifiers(), _rName, _rType, _rDesc, _rSchema, _rCatalog )
74 : ,m_xColumnDefinitions(_xColumnDefinitions)
75 0 : ,m_nPrivileges(0)
76 : {
77 : SAL_INFO("dbaccess", "ODBTable::ODBTable" );
78 : OSL_ENSURE(getMetaData().is(), "ODBTable::ODBTable : invalid conn !");
79 : OSL_ENSURE(!_rName.isEmpty(), "ODBTable::ODBTable : name !");
80 : // TODO : think about collecting the privileges here, as we can't ensure that in getFastPropertyValue, where
81 : // we do this at the moment, the statement needed can be supplied by the connection (for example the SQL-Server
82 : // ODBC driver does not allow more than one statement per connection, and in getFastPropertyValue it's more
83 : // likely that it's already used up than it's here.)
84 0 : }
85 :
86 0 : ODBTable::ODBTable(connectivity::sdbcx::OCollection* _pTables
87 : ,const Reference< XConnection >& _rxConn)
88 : throw(SQLException)
89 0 : :OTable_Base(_pTables,_rxConn, _rxConn->getMetaData().is() && _rxConn->getMetaData()->supportsMixedCaseQuotedIdentifiers())
90 0 : ,m_nPrivileges(-1)
91 : {
92 : SAL_INFO("dbaccess", "ODBTable::ODBTable" );
93 0 : }
94 :
95 0 : ODBTable::~ODBTable()
96 : {
97 0 : }
98 :
99 0 : IMPLEMENT_FORWARD_REFCOUNT(ODBTable,OTable_Base)
100 :
101 0 : OColumn* ODBTable::createColumn(const OUString& _rName) const
102 : {
103 : SAL_INFO("dbaccess", "ODBTable::createColumn" );
104 0 : OColumn* pReturn = NULL;
105 :
106 0 : Reference<XPropertySet> xProp;
107 0 : if ( m_xDriverColumns.is() && m_xDriverColumns->hasByName(_rName) )
108 : {
109 0 : xProp.set(m_xDriverColumns->getByName(_rName),UNO_QUERY);
110 : }
111 : else
112 : {
113 0 : OColumns* pColumns = static_cast<OColumns*>(m_pColumns);
114 0 : xProp.set(pColumns->createBaseObject(_rName),UNO_QUERY);
115 : }
116 :
117 0 : Reference<XPropertySet> xColumnDefintion;
118 0 : if ( m_xColumnDefinitions.is() && m_xColumnDefinitions->hasByName(_rName) )
119 0 : xColumnDefintion.set(m_xColumnDefinitions->getByName(_rName),UNO_QUERY);
120 0 : pReturn = new OTableColumnWrapper( xProp, xColumnDefintion, false );
121 :
122 0 : return pReturn;
123 : }
124 :
125 0 : void ODBTable::columnAppended( const Reference< XPropertySet >& /*_rxSourceDescriptor*/ )
126 : {
127 : SAL_INFO("dbaccess", "ODBTable::columnAppended" );
128 : // not interested in
129 0 : }
130 :
131 0 : void ODBTable::columnDropped(const OUString& _sName)
132 : {
133 : SAL_INFO("dbaccess", "ODBTable::columnDropped" );
134 0 : Reference<XDrop> xDrop(m_xColumnDefinitions,UNO_QUERY);
135 0 : if ( xDrop.is() && m_xColumnDefinitions->hasByName(_sName) )
136 : {
137 0 : xDrop->dropByName(_sName);
138 0 : }
139 0 : }
140 :
141 0 : Sequence< sal_Int8 > ODBTable::getImplementationId() throw (RuntimeException, std::exception)
142 : {
143 0 : return css::uno::Sequence<sal_Int8>();
144 : }
145 :
146 : // OComponentHelper
147 0 : void SAL_CALL ODBTable::disposing()
148 : {
149 : SAL_INFO("dbaccess", "ODBTable::disposing" );
150 0 : OPropertySetHelper::disposing();
151 0 : OTable_Base::disposing();
152 0 : m_xColumnDefinitions = NULL;
153 0 : m_xDriverColumns = NULL;
154 0 : m_pColumnMediator = NULL;
155 0 : }
156 :
157 0 : void ODBTable::getFastPropertyValue(Any& _rValue, sal_Int32 _nHandle) const
158 : {
159 : SAL_INFO("dbaccess", "ODBTable::getFastPropertyValue" );
160 0 : if ((PROPERTY_ID_PRIVILEGES == _nHandle) && (-1 == m_nPrivileges))
161 : { // somebody is asking for the privileges an we do not know them, yet
162 0 : const_cast<ODBTable*>(this)->m_nPrivileges = ::dbtools::getTablePrivileges(getMetaData(),m_CatalogName,m_SchemaName, m_Name);
163 : }
164 :
165 0 : OTable_Base::getFastPropertyValue(_rValue, _nHandle);
166 0 : }
167 :
168 0 : void ODBTable::construct()
169 : {
170 : SAL_INFO("dbaccess", "ODBTable::construct" );
171 0 : ::osl::MutexGuard aGuard(m_aMutex);
172 :
173 : // we don't collect the privileges here, this is potentially expensive. Instead we determine them on request.
174 : // (see getFastPropertyValue)
175 0 : m_nPrivileges = -1;
176 :
177 0 : OTable_Base::construct();
178 :
179 : registerProperty(PROPERTY_FILTER, PROPERTY_ID_FILTER, PropertyAttribute::BOUND,
180 0 : &m_sFilter, ::getCppuType(&m_sFilter));
181 :
182 : registerProperty(PROPERTY_ORDER, PROPERTY_ID_ORDER, PropertyAttribute::BOUND,
183 0 : &m_sOrder, ::getCppuType(&m_sOrder));
184 :
185 : registerProperty(PROPERTY_APPLYFILTER, PROPERTY_ID_APPLYFILTER, PropertyAttribute::BOUND,
186 0 : &m_bApplyFilter, ::getBooleanCppuType());
187 :
188 : registerProperty(PROPERTY_FONT, PROPERTY_ID_FONT, PropertyAttribute::BOUND,
189 0 : &m_aFont, ::getCppuType(&m_aFont));
190 :
191 : registerMayBeVoidProperty(PROPERTY_ROW_HEIGHT, PROPERTY_ID_ROW_HEIGHT, PropertyAttribute::BOUND | PropertyAttribute::MAYBEVOID,
192 0 : &m_aRowHeight, ::getCppuType(static_cast<sal_Int32*>(NULL)));
193 :
194 : registerMayBeVoidProperty(PROPERTY_TEXTCOLOR, PROPERTY_ID_TEXTCOLOR, PropertyAttribute::BOUND | PropertyAttribute::MAYBEVOID,
195 0 : &m_aTextColor, ::getCppuType(static_cast<sal_Int32*>(NULL)));
196 :
197 : registerProperty(PROPERTY_PRIVILEGES, PROPERTY_ID_PRIVILEGES, PropertyAttribute::BOUND | PropertyAttribute::READONLY,
198 0 : &m_nPrivileges, ::getCppuType(static_cast<sal_Int32*>(NULL)));
199 :
200 : registerMayBeVoidProperty(PROPERTY_TEXTLINECOLOR, PROPERTY_ID_TEXTLINECOLOR, PropertyAttribute::BOUND | PropertyAttribute::MAYBEVOID,
201 0 : &m_aTextLineColor, ::getCppuType(static_cast<sal_Int32*>(NULL)));
202 :
203 : registerProperty(PROPERTY_TEXTEMPHASIS, PROPERTY_ID_TEXTEMPHASIS, PropertyAttribute::BOUND,
204 0 : &m_nFontEmphasis, ::getCppuType(&m_nFontEmphasis));
205 :
206 : registerProperty(PROPERTY_TEXTRELIEF, PROPERTY_ID_TEXTRELIEF, PropertyAttribute::BOUND,
207 0 : &m_nFontRelief, ::getCppuType(&m_nFontRelief));
208 :
209 0 : registerProperty(PROPERTY_FONTNAME, PROPERTY_ID_FONTNAME, PropertyAttribute::BOUND,&m_aFont.Name, ::getCppuType(&m_aFont.Name));
210 0 : registerProperty(PROPERTY_FONTHEIGHT, PROPERTY_ID_FONTHEIGHT, PropertyAttribute::BOUND,&m_aFont.Height, ::getCppuType(&m_aFont.Height));
211 0 : registerProperty(PROPERTY_FONTWIDTH, PROPERTY_ID_FONTWIDTH, PropertyAttribute::BOUND,&m_aFont.Width, ::getCppuType(&m_aFont.Width));
212 0 : registerProperty(PROPERTY_FONTSTYLENAME, PROPERTY_ID_FONTSTYLENAME, PropertyAttribute::BOUND,&m_aFont.StyleName, ::getCppuType(&m_aFont.StyleName));
213 0 : registerProperty(PROPERTY_FONTFAMILY, PROPERTY_ID_FONTFAMILY, PropertyAttribute::BOUND,&m_aFont.Family, ::getCppuType(&m_aFont.Family));
214 0 : registerProperty(PROPERTY_FONTCHARSET, PROPERTY_ID_FONTCHARSET, PropertyAttribute::BOUND,&m_aFont.CharSet, ::getCppuType(&m_aFont.CharSet));
215 0 : registerProperty(PROPERTY_FONTPITCH, PROPERTY_ID_FONTPITCH, PropertyAttribute::BOUND,&m_aFont.Pitch, ::getCppuType(&m_aFont.Pitch));
216 0 : registerProperty(PROPERTY_FONTCHARWIDTH, PROPERTY_ID_FONTCHARWIDTH, PropertyAttribute::BOUND,&m_aFont.CharacterWidth, ::getCppuType(&m_aFont.CharacterWidth));
217 0 : registerProperty(PROPERTY_FONTWEIGHT, PROPERTY_ID_FONTWEIGHT, PropertyAttribute::BOUND,&m_aFont.Weight, ::getCppuType(&m_aFont.Weight));
218 0 : registerProperty(PROPERTY_FONTSLANT, PROPERTY_ID_FONTSLANT, PropertyAttribute::BOUND,&m_aFont.Slant, ::getCppuType(&m_aFont.Slant));
219 0 : registerProperty(PROPERTY_FONTUNDERLINE, PROPERTY_ID_FONTUNDERLINE, PropertyAttribute::BOUND,&m_aFont.Underline, ::getCppuType(&m_aFont.Underline));
220 0 : registerProperty(PROPERTY_FONTSTRIKEOUT, PROPERTY_ID_FONTSTRIKEOUT, PropertyAttribute::BOUND,&m_aFont.Strikeout, ::getCppuType(&m_aFont.Strikeout));
221 0 : registerProperty(PROPERTY_FONTORIENTATION, PROPERTY_ID_FONTORIENTATION, PropertyAttribute::BOUND,&m_aFont.Orientation, ::getCppuType(&m_aFont.Orientation));
222 0 : registerProperty(PROPERTY_FONTKERNING, PROPERTY_ID_FONTKERNING, PropertyAttribute::BOUND,&m_aFont.Kerning, ::getCppuType(&m_aFont.Kerning));
223 0 : registerProperty(PROPERTY_FONTWORDLINEMODE, PROPERTY_ID_FONTWORDLINEMODE,PropertyAttribute::BOUND,&m_aFont.WordLineMode, ::getCppuType(&m_aFont.WordLineMode));
224 0 : registerProperty(PROPERTY_FONTTYPE, PROPERTY_ID_FONTTYPE, PropertyAttribute::BOUND,&m_aFont.Type, ::getCppuType(&m_aFont.Type));
225 :
226 0 : refreshColumns();
227 0 : }
228 :
229 0 : ::cppu::IPropertyArrayHelper* ODBTable::createArrayHelper( sal_Int32 _nId) const
230 : {
231 : SAL_INFO("dbaccess", "ODBTable::createArrayHelper" );
232 0 : Sequence< Property > aProps;
233 0 : describeProperties(aProps);
234 0 : if(!_nId)
235 : {
236 0 : Property* pIter = aProps.getArray();
237 0 : Property* pEnd = pIter + aProps.getLength();
238 0 : for(;pIter != pEnd;++pIter)
239 : {
240 0 : if (pIter->Name == PROPERTY_CATALOGNAME)
241 0 : pIter->Attributes = PropertyAttribute::READONLY;
242 0 : else if (pIter->Name == PROPERTY_SCHEMANAME)
243 0 : pIter->Attributes = PropertyAttribute::READONLY;
244 0 : else if (pIter->Name == PROPERTY_DESCRIPTION)
245 0 : pIter->Attributes = PropertyAttribute::READONLY;
246 0 : else if (pIter->Name == PROPERTY_NAME)
247 0 : pIter->Attributes = PropertyAttribute::READONLY;
248 : }
249 : }
250 :
251 0 : return new ::cppu::OPropertyArrayHelper(aProps);
252 : }
253 :
254 0 : ::cppu::IPropertyArrayHelper & SAL_CALL ODBTable::getInfoHelper()
255 : {
256 0 : return *ODBTable_PROP::getArrayHelper(isNew() ? 1 : 0);
257 : }
258 :
259 : // XServiceInfo
260 0 : IMPLEMENT_SERVICE_INFO1(ODBTable, "com.sun.star.sdb.dbaccess.ODBTable", SERVICE_SDBCX_TABLE)
261 :
262 0 : Any SAL_CALL ODBTable::queryInterface( const Type & rType ) throw(RuntimeException, std::exception)
263 : {
264 : SAL_INFO("dbaccess", "ODBTable::getInfoHelper" );
265 0 : if(rType == getCppuType( (Reference<XRename>*)0) && !getRenameService().is() )
266 0 : return Any();
267 0 : if(rType == getCppuType( (Reference<XAlterTable>*)0) && !getAlterService().is() )
268 0 : return Any();
269 0 : return OTable_Base::queryInterface( rType);
270 : }
271 :
272 0 : Sequence< Type > SAL_CALL ODBTable::getTypes( ) throw(RuntimeException, std::exception)
273 : {
274 : SAL_INFO("dbaccess", "ODBTable::getTypes" );
275 0 : Type aRenameType = getCppuType( (Reference<XRename>*)0);
276 0 : Type aAlterType = getCppuType( (Reference<XAlterTable>*)0);
277 :
278 0 : Sequence< Type > aTypes(OTable_Base::getTypes());
279 0 : ::std::vector<Type> aOwnTypes;
280 0 : aOwnTypes.reserve(aTypes.getLength());
281 :
282 0 : const Type* pIter = aTypes.getConstArray();
283 0 : const Type* pEnd = pIter + aTypes.getLength();
284 0 : for(;pIter != pEnd ;++pIter)
285 : {
286 0 : if( (*pIter != aRenameType || getRenameService().is()) && (*pIter != aAlterType || getAlterService().is()))
287 0 : aOwnTypes.push_back(*pIter);
288 : }
289 :
290 0 : Type* pTypes = aOwnTypes.empty() ? 0 : &aOwnTypes[0];
291 0 : return Sequence< Type >(pTypes, aOwnTypes.size());
292 : }
293 :
294 : // XRename,
295 0 : void SAL_CALL ODBTable::rename( const OUString& _rNewName ) throw(SQLException, ElementExistException, RuntimeException, std::exception)
296 : {
297 : SAL_INFO("dbaccess", "ODBTable::rename" );
298 0 : ::osl::MutexGuard aGuard(m_aMutex);
299 0 : checkDisposed(connectivity::sdbcx::OTableDescriptor_BASE::rBHelper.bDisposed);
300 0 : if ( !getRenameService().is() )
301 0 : throw SQLException(DBACORE_RESSTRING(RID_STR_NO_TABLE_RENAME),*this,SQLSTATE_GENERAL,1000,Any() );
302 :
303 0 : Reference<XPropertySet> xTable(this);
304 0 : getRenameService()->rename(xTable,_rNewName);
305 0 : ::connectivity::OTable_TYPEDEF::rename(_rNewName);
306 0 : }
307 :
308 : // XAlterTable,
309 0 : void SAL_CALL ODBTable::alterColumnByName( const OUString& _rName, const Reference< XPropertySet >& _rxDescriptor ) throw(SQLException, NoSuchElementException, RuntimeException, std::exception)
310 : {
311 : SAL_INFO("dbaccess", "ODBTable::alterColumnByName" );
312 0 : ::osl::MutexGuard aGuard(m_aMutex);
313 0 : checkDisposed(connectivity::sdbcx::OTableDescriptor_BASE::rBHelper.bDisposed);
314 0 : if ( !getAlterService().is() )
315 0 : throw SQLException(DBACORE_RESSTRING(RID_STR_NO_TABLE_RENAME),*this,SQLSTATE_GENERAL,1000,Any() );
316 :
317 0 : if ( !m_pColumns->hasByName(_rName) )
318 0 : throw SQLException(DBACORE_RESSTRING(RID_STR_COLUMN_NOT_VALID),*this,SQLSTATE_GENERAL,1000,Any() );
319 :
320 0 : Reference<XPropertySet> xTable(this);
321 0 : getAlterService()->alterColumnByName(xTable,_rName,_rxDescriptor);
322 0 : m_pColumns->refresh();
323 0 : }
324 :
325 0 : sal_Int64 SAL_CALL ODBTable::getSomething( const Sequence< sal_Int8 >& rId ) throw(RuntimeException, std::exception)
326 : {
327 : SAL_INFO("dbaccess", "ODBTable::getSomething" );
328 0 : sal_Int64 nRet(0);
329 0 : if (rId.getLength() == 16 && 0 == memcmp(getUnoTunnelImplementationId().getConstArray(), rId.getConstArray(), 16 ) )
330 0 : nRet = reinterpret_cast<sal_Int64>(this);
331 : else
332 0 : nRet = OTable_Base::getSomething(rId);
333 :
334 0 : return nRet;
335 : }
336 :
337 0 : Sequence< sal_Int8 > ODBTable::getUnoTunnelImplementationId()
338 : {
339 : SAL_INFO("dbaccess", "ODBTable::getUnoTunnelImplementationId" );
340 : static ::cppu::OImplementationId * pId = 0;
341 0 : if (! pId)
342 : {
343 0 : ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
344 0 : if (! pId)
345 : {
346 0 : static ::cppu::OImplementationId aId;
347 0 : pId = &aId;
348 0 : }
349 : }
350 0 : return pId->getImplementationId();
351 : }
352 :
353 0 : Reference< XPropertySet > ODBTable::createColumnDescriptor()
354 : {
355 : SAL_INFO("dbaccess", "ODBTable::createColumnDescriptor" );
356 0 : return new OTableColumnDescriptor( true );
357 : }
358 :
359 0 : sdbcx::OCollection* ODBTable::createColumns(const TStringVector& _rNames)
360 : {
361 : SAL_INFO("dbaccess", "ODBTable::createColumns" );
362 0 : Reference<XDatabaseMetaData> xMeta = getMetaData();
363 0 : OColumns* pCol = new OColumns(*this, m_aMutex, NULL, isCaseSensitive(), _rNames, this,this,
364 0 : getAlterService().is() || (xMeta.is() && xMeta->supportsAlterTableWithAddColumn()),
365 0 : getAlterService().is() || (xMeta.is() && xMeta->supportsAlterTableWithDropColumn()));
366 0 : static_cast<OColumnsHelper*>(pCol)->setParent(this);
367 0 : pCol->setParent(*this);
368 0 : m_pColumnMediator = new OContainerMediator( pCol, m_xColumnDefinitions, getConnection() );
369 0 : pCol->setMediator( m_pColumnMediator.get() );
370 0 : return pCol;
371 : }
372 :
373 0 : sdbcx::OCollection* ODBTable::createKeys(const TStringVector& _rNames)
374 : {
375 : SAL_INFO("dbaccess", "ODBTable::createKeys" );
376 0 : return new connectivity::OKeysHelper(this,m_aMutex,_rNames);
377 : }
378 :
379 0 : sdbcx::OCollection* ODBTable::createIndexes(const TStringVector& _rNames)
380 : {
381 : SAL_INFO("dbaccess", "ODBTable::createIndexes" );
382 0 : return new OIndexes(this,m_aMutex,_rNames,NULL);
383 : }
384 :
385 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|