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 "tablecontainer.hxx"
21 : #include "dbastrings.hrc"
22 : #include "table.hxx"
23 : #include <comphelper/property.hxx>
24 : #include <comphelper/processfactory.hxx>
25 : #include <tools/debug.hxx>
26 : #include <comphelper/enumhelper.hxx>
27 : #include "core_resource.hxx"
28 : #include "core_resource.hrc"
29 : #include <com/sun/star/sdb/CommandType.hpp>
30 : #include <com/sun/star/beans/XPropertySet.hpp>
31 : #include <com/sun/star/beans/PropertyState.hpp>
32 : #include <com/sun/star/beans/XPropertyState.hpp>
33 : #include <com/sun/star/sdb/TableDefinition.hpp>
34 : #include <com/sun/star/sdbc/XConnection.hpp>
35 : #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
36 : #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
37 : #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
38 : #include <com/sun/star/sdbc/KeyRule.hpp>
39 : #include <com/sun/star/sdbcx/KeyType.hpp>
40 : #include <com/sun/star/sdbc/ColumnValue.hpp>
41 : #include <com/sun/star/sdbc/XRow.hpp>
42 : #include <comphelper/types.hxx>
43 : #include <connectivity/dbtools.hxx>
44 : #include <comphelper/extract.hxx>
45 : #include <connectivity/dbexception.hxx>
46 : #include "TableDeco.hxx"
47 : #include "sdbcoretools.hxx"
48 : #include "ContainerMediator.hxx"
49 : #include "definitioncolumn.hxx"
50 : #include "objectnameapproval.hxx"
51 : #include <tools/diagnose_ex.h>
52 :
53 : using namespace dbaccess;
54 : using namespace dbtools;
55 : using namespace ::com::sun::star::uno;
56 : using namespace ::com::sun::star::lang;
57 : using namespace ::com::sun::star::beans;
58 : using namespace ::com::sun::star::sdbc;
59 : using namespace ::com::sun::star::sdb;
60 : using namespace ::com::sun::star::sdbcx;
61 : using namespace ::com::sun::star::container;
62 : using namespace ::com::sun::star::util;
63 : using namespace ::osl;
64 : using namespace ::comphelper;
65 : using namespace ::cppu;
66 : using namespace ::connectivity::sdbcx;
67 :
68 : namespace
69 : {
70 0 : sal_Bool lcl_isPropertySetDefaulted(const Sequence< OUString>& _aNames,const Reference<XPropertySet>& _xProp)
71 : {
72 0 : Reference<XPropertyState> xState(_xProp,UNO_QUERY);
73 0 : if ( xState.is() )
74 : {
75 0 : const OUString* pIter = _aNames.getConstArray();
76 0 : const OUString* pEnd = pIter + _aNames.getLength();
77 0 : for(;pIter != pEnd;++pIter)
78 : {
79 : try
80 : {
81 0 : PropertyState aState = xState->getPropertyState(*pIter);
82 0 : if ( aState != PropertyState_DEFAULT_VALUE )
83 0 : break;
84 : }
85 0 : catch(const Exception&)
86 : {
87 : SAL_WARN("dbaccess", "lcl_isPropertySetDefaulted: Exception caught!" );
88 : }
89 : }
90 0 : return ( pIter == pEnd );
91 : }
92 0 : return sal_False;
93 : }
94 : }
95 :
96 : // OTableContainer
97 :
98 0 : OTableContainer::OTableContainer(::cppu::OWeakObject& _rParent,
99 : ::osl::Mutex& _rMutex,
100 : const Reference< XConnection >& _xCon,
101 : sal_Bool _bCase,
102 : const Reference< XNameContainer >& _xTableDefinitions,
103 : IRefreshListener* _pRefreshListener,
104 : ::dbtools::IWarningsContainer* _pWarningsContainer
105 : ,oslInterlockedCount& _nInAppend)
106 : :OFilteredContainer(_rParent,_rMutex,_xCon,_bCase,_pRefreshListener,_pWarningsContainer,_nInAppend)
107 : ,m_xTableDefinitions(_xTableDefinitions)
108 : ,m_pTableMediator( NULL )
109 0 : ,m_bInDrop(sal_False)
110 : {
111 0 : }
112 :
113 0 : OTableContainer::~OTableContainer()
114 : {
115 : SAL_INFO("dbaccess", "dbaccess Ocke.Janssen@sun.com OTableContainer::OTableContainer" );
116 0 : }
117 :
118 0 : void OTableContainer::removeMasterContainerListener()
119 : {
120 : SAL_INFO("dbaccess", "dbaccess Ocke.Janssen@sun.com OTableContainer::removeMasterContainerListener" );
121 : try
122 : {
123 0 : Reference<XContainer> xCont( m_xMasterContainer, UNO_QUERY_THROW );
124 0 : xCont->removeContainerListener( this );
125 : }
126 0 : catch( const Exception& )
127 : {
128 : DBG_UNHANDLED_EXCEPTION();
129 : }
130 0 : }
131 :
132 0 : OUString OTableContainer::getTableTypeRestriction() const
133 : {
134 : // no restriction at all (other than the ones provided externally)
135 0 : return OUString();
136 : }
137 :
138 : // XServiceInfo
139 0 : IMPLEMENT_SERVICE_INFO2(OTableContainer, "com.sun.star.sdb.dbaccess.OTableContainer", SERVICE_SDBCX_CONTAINER, SERVICE_SDBCX_TABLES)
140 :
141 : namespace
142 : {
143 0 : void lcl_createDefintionObject(const OUString& _rName
144 : ,const Reference< XNameContainer >& _xTableDefinitions
145 : ,Reference<XPropertySet>& _xTableDefinition
146 : ,Reference<XNameAccess>& _xColumnDefinitions)
147 : {
148 0 : if ( _xTableDefinitions.is() )
149 : {
150 0 : if ( _xTableDefinitions->hasByName(_rName) )
151 0 : _xTableDefinition.set(_xTableDefinitions->getByName(_rName),UNO_QUERY);
152 : else
153 : {
154 : // set as folder
155 0 : _xTableDefinition.set( TableDefinition::createWithName( ::comphelper::getProcessComponentContext(), _rName ), UNO_QUERY);
156 0 : _xTableDefinitions->insertByName(_rName,makeAny(_xTableDefinition));
157 : }
158 0 : Reference<XColumnsSupplier> xColumnsSupplier(_xTableDefinition,UNO_QUERY);
159 0 : if ( xColumnsSupplier.is() )
160 0 : _xColumnDefinitions = xColumnsSupplier->getColumns();
161 : }
162 0 : }
163 :
164 : }
165 :
166 0 : connectivity::sdbcx::ObjectType OTableContainer::createObject(const OUString& _rName)
167 : {
168 : SAL_INFO("dbaccess", "dbaccess Ocke.Janssen@sun.com OTableContainer::createObject" );
169 0 : Reference<XColumnsSupplier > xSup;
170 0 : if(m_xMasterContainer.is() && m_xMasterContainer->hasByName(_rName))
171 0 : xSup.set(m_xMasterContainer->getByName(_rName),UNO_QUERY);
172 :
173 0 : connectivity::sdbcx::ObjectType xRet;
174 0 : if ( m_xMetaData.is() )
175 : {
176 0 : Reference<XPropertySet> xTableDefinition;
177 0 : Reference<XNameAccess> xColumnDefinitions;
178 0 : lcl_createDefintionObject(_rName,m_xTableDefinitions,xTableDefinition,xColumnDefinitions);
179 :
180 0 : if ( xSup.is() )
181 : {
182 0 : ODBTableDecorator* pTable = new ODBTableDecorator( m_xConnection, xSup, ::dbtools::getNumberFormats( m_xConnection ) ,xColumnDefinitions);
183 0 : xRet = pTable;
184 0 : pTable->construct();
185 : }
186 : else
187 : {
188 0 : OUString sCatalog,sSchema,sTable;
189 : ::dbtools::qualifiedNameComponents(m_xMetaData,
190 : _rName,
191 : sCatalog,
192 : sSchema,
193 : sTable,
194 0 : ::dbtools::eInDataManipulation);
195 0 : Any aCatalog;
196 0 : if(!sCatalog.isEmpty())
197 0 : aCatalog <<= sCatalog;
198 0 : OUString sType,sDescription;
199 0 : Sequence< OUString> aTypeFilter;
200 0 : getAllTableTypeFilter( aTypeFilter );
201 :
202 0 : Reference< XResultSet > xRes = m_xMetaData.is() ? m_xMetaData->getTables(aCatalog,sSchema,sTable,aTypeFilter) : Reference< XResultSet >();
203 0 : if(xRes.is() && xRes->next())
204 : {
205 0 : Reference< XRow > xRow(xRes,UNO_QUERY);
206 0 : if(xRow.is())
207 : {
208 0 : sType = xRow->getString(4);
209 0 : sDescription = xRow->getString(5);
210 0 : }
211 : }
212 0 : ::comphelper::disposeComponent(xRes);
213 : ODBTable* pTable = new ODBTable(this
214 : ,m_xConnection
215 : ,sCatalog
216 : ,sSchema
217 : ,sTable
218 : ,sType
219 : ,sDescription
220 0 : ,xColumnDefinitions);
221 0 : xRet = pTable;
222 0 : pTable->construct();
223 : }
224 0 : Reference<XPropertySet> xDest(xRet,UNO_QUERY);
225 0 : if ( xTableDefinition.is() )
226 0 : ::comphelper::copyProperties(xTableDefinition,xDest);
227 :
228 0 : if ( !m_pTableMediator.is() )
229 0 : m_pTableMediator = new OContainerMediator(
230 0 : this, m_xTableDefinitions.get(), m_xConnection );
231 0 : if ( m_pTableMediator.is() )
232 0 : m_pTableMediator->notifyElementCreated(_rName,xDest);
233 : }
234 :
235 0 : return xRet;
236 : }
237 :
238 0 : Reference< XPropertySet > OTableContainer::createDescriptor()
239 : {
240 : SAL_INFO("dbaccess", "dbaccess Ocke.Janssen@sun.com OTableContainer::createDescriptor" );
241 0 : Reference< XPropertySet > xRet;
242 :
243 : // first we have to look if the master tables support this
244 : // and if so then create a table object as well with the master tables
245 0 : Reference<XColumnsSupplier > xMasterColumnsSup;
246 0 : Reference<XDataDescriptorFactory> xDataFactory(m_xMasterContainer,UNO_QUERY);
247 0 : if ( xDataFactory.is() && m_xMetaData.is() )
248 : {
249 0 : xMasterColumnsSup = Reference< XColumnsSupplier >( xDataFactory->createDataDescriptor(), UNO_QUERY );
250 0 : ODBTableDecorator* pTable = new ODBTableDecorator( m_xConnection, xMasterColumnsSup, ::dbtools::getNumberFormats( m_xConnection ) ,NULL);
251 0 : xRet = pTable;
252 0 : pTable->construct();
253 : }
254 : else
255 : {
256 0 : ODBTable* pTable = new ODBTable(this, m_xConnection);
257 0 : xRet = pTable;
258 0 : pTable->construct();
259 : }
260 0 : return xRet;
261 : }
262 :
263 : // XAppend
264 0 : ObjectType OTableContainer::appendObject( const OUString& _rForName, const Reference< XPropertySet >& descriptor )
265 : {
266 : SAL_INFO("dbaccess", "dbaccess Ocke.Janssen@sun.com OTableContainer::appendObject" );
267 : // append the new table with a create stmt
268 0 : OUString aName = getString(descriptor->getPropertyValue(PROPERTY_NAME));
269 0 : if(m_xMasterContainer.is() && m_xMasterContainer->hasByName(aName))
270 : {
271 0 : OUString sMessage(DBACORE_RESSTRING(RID_STR_TABLE_IS_FILTERED));
272 0 : throw SQLException(sMessage.replaceAll("$name$", aName),static_cast<XTypeProvider*>(static_cast<OFilteredContainer*>(this)),SQLSTATE_GENERAL,1000,Any());
273 : }
274 :
275 0 : Reference< XConnection > xConnection( m_xConnection.get(), UNO_QUERY );
276 0 : PContainerApprove pApprove( new ObjectNameApproval( xConnection, ObjectNameApproval::TypeTable ) );
277 0 : pApprove->approveElement( aName, descriptor );
278 :
279 : try
280 : {
281 0 : EnsureReset aReset(m_nInAppend);
282 0 : Reference<XAppend> xAppend(m_xMasterContainer,UNO_QUERY);
283 0 : if(xAppend.is())
284 : {
285 0 : xAppend->appendByDescriptor(descriptor);
286 : }
287 : else
288 : {
289 0 : OUString aSql = ::dbtools::createSqlCreateTableStatement(descriptor,m_xConnection);
290 :
291 0 : Reference<XConnection> xCon = m_xConnection;
292 : OSL_ENSURE(xCon.is(),"Connection is null!");
293 0 : if ( xCon.is() )
294 : {
295 0 : Reference< XStatement > xStmt = xCon->createStatement( );
296 0 : if ( xStmt.is() )
297 0 : xStmt->execute(aSql);
298 0 : ::comphelper::disposeComponent(xStmt);
299 0 : }
300 0 : }
301 : }
302 0 : catch(const Exception&)
303 : {
304 0 : throw;
305 : }
306 :
307 0 : Reference<XPropertySet> xTableDefinition;
308 0 : Reference<XNameAccess> xColumnDefinitions;
309 0 : lcl_createDefintionObject(getNameForObject(descriptor),m_xTableDefinitions,xTableDefinition,xColumnDefinitions);
310 0 : Reference<XColumnsSupplier> xSup(descriptor,UNO_QUERY);
311 0 : Reference<XDataDescriptorFactory> xFac(xColumnDefinitions,UNO_QUERY);
312 0 : Reference<XAppend> xAppend(xColumnDefinitions,UNO_QUERY);
313 0 : sal_Bool bModified = sal_False;
314 0 : if ( xSup.is() && xColumnDefinitions.is() && xFac.is() && xAppend.is() )
315 : {
316 0 : Reference<XNameAccess> xNames = xSup->getColumns();
317 0 : if ( xNames.is() )
318 : {
319 0 : Reference<XPropertySet> xProp = xFac->createDataDescriptor();
320 0 : Sequence< OUString> aSeq = xNames->getElementNames();
321 0 : const OUString* pIter = aSeq.getConstArray();
322 0 : const OUString* pEnd = pIter + aSeq.getLength();
323 0 : for(;pIter != pEnd;++pIter)
324 : {
325 0 : if ( !xColumnDefinitions->hasByName(*pIter) )
326 : {
327 0 : Reference<XPropertySet> xColumn(xNames->getByName(*pIter),UNO_QUERY);
328 0 : if ( !OColumnSettings::hasDefaultSettings( xColumn ) )
329 : {
330 0 : ::comphelper::copyProperties( xColumn, xProp );
331 0 : xAppend->appendByDescriptor( xProp );
332 0 : bModified = sal_True;
333 0 : }
334 : }
335 0 : }
336 0 : }
337 : }
338 : const static OUString s_pTableProps[] = { OUString(PROPERTY_FILTER), OUString(PROPERTY_ORDER)
339 : , OUString(PROPERTY_APPLYFILTER), OUString(PROPERTY_FONT)
340 : , OUString(PROPERTY_ROW_HEIGHT), OUString(PROPERTY_TEXTCOLOR)
341 : , OUString(PROPERTY_TEXTLINECOLOR), OUString(PROPERTY_TEXTEMPHASIS)
342 0 : , OUString(PROPERTY_TEXTRELIEF) };
343 0 : Sequence< OUString> aNames(s_pTableProps,sizeof(s_pTableProps)/sizeof(s_pTableProps[0]));
344 0 : if ( bModified || !lcl_isPropertySetDefaulted(aNames,xTableDefinition) )
345 0 : ::dbaccess::notifyDataSourceModified(m_xTableDefinitions,sal_True);
346 :
347 0 : return createObject( _rForName );
348 : }
349 :
350 : // XDrop
351 0 : void OTableContainer::dropObject(sal_Int32 _nPos, const OUString& _sElementName)
352 : {
353 : SAL_INFO("dbaccess", "dbaccess Ocke.Janssen@sun.com OTableContainer::dropObject" );
354 0 : m_bInDrop = sal_True;
355 : try
356 : {
357 0 : Reference< XDrop > xDrop(m_xMasterContainer,UNO_QUERY);
358 0 : if(xDrop.is())
359 0 : xDrop->dropByName(_sElementName);
360 : else
361 : {
362 0 : OUString sCatalog,sSchema,sTable,sComposedName;
363 :
364 0 : sal_Bool bIsView = sal_False;
365 0 : Reference<XPropertySet> xTable(getObject(_nPos),UNO_QUERY);
366 0 : if ( xTable.is() && m_xMetaData.is() )
367 : {
368 0 : if( m_xMetaData.is() && m_xMetaData->supportsCatalogsInTableDefinitions() )
369 0 : xTable->getPropertyValue(PROPERTY_CATALOGNAME) >>= sCatalog;
370 0 : if( m_xMetaData.is() && m_xMetaData->supportsSchemasInTableDefinitions() )
371 0 : xTable->getPropertyValue(PROPERTY_SCHEMANAME) >>= sSchema;
372 0 : xTable->getPropertyValue(PROPERTY_NAME) >>= sTable;
373 :
374 0 : sComposedName = ::dbtools::composeTableName( m_xMetaData, sCatalog, sSchema, sTable, true, ::dbtools::eInTableDefinitions );
375 :
376 0 : OUString sType;
377 0 : xTable->getPropertyValue(PROPERTY_TYPE) >>= sType;
378 0 : bIsView = sType.equalsIgnoreAsciiCase("VIEW");
379 : }
380 :
381 0 : if(sComposedName.isEmpty())
382 0 : ::dbtools::throwFunctionSequenceException(static_cast<XTypeProvider*>(static_cast<OFilteredContainer*>(this)));
383 :
384 0 : OUString aSql("DROP ");
385 :
386 0 : if ( bIsView ) // here we have a view
387 0 : aSql += "VIEW ";
388 : else
389 0 : aSql += "TABLE ";
390 0 : aSql += sComposedName;
391 0 : Reference<XConnection> xCon = m_xConnection;
392 : OSL_ENSURE(xCon.is(),"Connection is null!");
393 0 : if ( xCon.is() )
394 : {
395 0 : Reference< XStatement > xStmt = xCon->createStatement( );
396 0 : if(xStmt.is())
397 0 : xStmt->execute(aSql);
398 0 : ::comphelper::disposeComponent(xStmt);
399 0 : }
400 : }
401 :
402 0 : if ( m_xTableDefinitions.is() && m_xTableDefinitions->hasByName(_sElementName) )
403 : {
404 0 : m_xTableDefinitions->removeByName(_sElementName);
405 0 : }
406 : }
407 0 : catch(const Exception&)
408 : {
409 0 : m_bInDrop = sal_False;
410 0 : throw;
411 : }
412 0 : m_bInDrop = sal_False;
413 0 : }
414 :
415 0 : void SAL_CALL OTableContainer::elementInserted( const ContainerEvent& Event ) throw (RuntimeException, std::exception)
416 : {
417 : SAL_INFO("dbaccess", "dbaccess Ocke.Janssen@sun.com OTableContainer::elementInserted" );
418 0 : ::osl::MutexGuard aGuard(m_rMutex);
419 0 : OUString sName;
420 0 : Event.Accessor >>= sName;
421 0 : if ( !m_nInAppend && !hasByName(sName) )
422 : {
423 0 : if(!m_xMasterContainer.is() || m_xMasterContainer->hasByName(sName))
424 : {
425 0 : ObjectType xName = createObject(sName);
426 0 : insertElement(sName,xName);
427 : // and notify our listeners
428 0 : ContainerEvent aEvent(static_cast<XContainer*>(this), makeAny(sName), makeAny(xName), Any());
429 0 : m_aContainerListeners.notifyEach( &XContainerListener::elementInserted, aEvent );
430 : }
431 0 : }
432 0 : }
433 :
434 0 : void SAL_CALL OTableContainer::elementRemoved( const ContainerEvent& /*Event*/ ) throw (RuntimeException, std::exception)
435 : {
436 : SAL_INFO("dbaccess", "dbaccess Ocke.Janssen@sun.com OTableContainer::elementRemoved" );
437 0 : }
438 :
439 0 : void SAL_CALL OTableContainer::elementReplaced( const ContainerEvent& Event ) throw (RuntimeException, std::exception)
440 : {
441 : SAL_INFO("dbaccess", "dbaccess Ocke.Janssen@sun.com OTableContainer::elementReplaced" );
442 : // create a new config entry
443 : {
444 0 : OUString sOldComposedName,sNewComposedName;
445 0 : Event.ReplacedElement >>= sOldComposedName;
446 0 : Event.Accessor >>= sNewComposedName;
447 :
448 0 : renameObject(sOldComposedName,sNewComposedName);
449 : }
450 0 : }
451 :
452 0 : void SAL_CALL OTableContainer::disposing()
453 : {
454 : SAL_INFO("dbaccess", "dbaccess Ocke.Janssen@sun.com OTableContainer::disposing" );
455 0 : OFilteredContainer::disposing();
456 : // say goodbye to our listeners
457 0 : m_xTableDefinitions = NULL;
458 0 : m_pTableMediator = NULL;
459 0 : }
460 :
461 0 : void SAL_CALL OTableContainer::disposing( const ::com::sun::star::lang::EventObject& /*Source*/ ) throw (::com::sun::star::uno::RuntimeException, std::exception)
462 : {
463 : SAL_INFO("dbaccess", "dbaccess Ocke.Janssen@sun.com OTableContainer::disposing" );
464 0 : }
465 :
466 0 : void OTableContainer::addMasterContainerListener()
467 : {
468 : try
469 : {
470 0 : Reference< XContainer > xCont( m_xMasterContainer, UNO_QUERY_THROW );
471 0 : xCont->addContainerListener( this );
472 : }
473 0 : catch( const Exception& )
474 : {
475 : DBG_UNHANDLED_EXCEPTION();
476 : }
477 0 : }
478 :
479 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|