1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <apitools.hxx>
21 #include <viewcontainer.hxx>
24 #include <comphelper/types.hxx>
25 #include <connectivity/dbtools.hxx>
26 #include <connectivity/dbexception.hxx>
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/sdbc/XConnection.hpp>
31 using namespace dbaccess
;
32 using namespace dbtools
;
33 using namespace ::com::sun::star::uno
;
34 using namespace ::com::sun::star::lang
;
35 using namespace ::com::sun::star::beans
;
36 using namespace ::com::sun::star::sdbc
;
37 using namespace ::com::sun::star::sdb
;
38 using namespace ::com::sun::star::sdbcx
;
39 using namespace ::com::sun::star::util
;
40 using namespace ::com::sun::star::container
;
41 using namespace ::osl
;
42 using namespace ::comphelper
;
43 using namespace ::cppu
;
44 using namespace ::connectivity::sdbcx
;
48 OViewContainer::OViewContainer(::cppu::OWeakObject
& _rParent
49 ,::osl::Mutex
& _rMutex
50 ,const Reference
< XConnection
>& _xCon
52 ,IRefreshListener
* _pRefreshListener
53 ,std::atomic
<std::size_t>& _nInAppend
)
54 :OFilteredContainer(_rParent
,_rMutex
,_xCon
,_bCase
,_pRefreshListener
,_nInAppend
)
55 ,m_bInElementRemoved(false)
59 OViewContainer::~OViewContainer()
64 IMPLEMENT_SERVICE_INFO2(OViewContainer
, "com.sun.star.sdb.dbaccess.OViewContainer", SERVICE_SDBCX_CONTAINER
, SERVICE_SDBCX_TABLES
)
66 ObjectType
OViewContainer::createObject(const OUString
& _rName
)
69 if ( m_xMasterContainer
.is() && m_xMasterContainer
->hasByName(_rName
) )
70 xProp
.set(m_xMasterContainer
->getByName(_rName
),UNO_QUERY
);
74 OUString sCatalog
,sSchema
,sTable
;
75 ::dbtools::qualifiedNameComponents(m_xMetaData
,
80 ::dbtools::EComposeRule::InDataManipulation
);
81 return new View(m_xConnection
,
92 Reference
< XPropertySet
> OViewContainer::createDescriptor()
94 Reference
< XPropertySet
> xRet
;
95 // first we have to look if the master tables support this
96 // and if so then create a table object as well with the master tables
97 Reference
<XDataDescriptorFactory
> xDataFactory(m_xMasterContainer
,UNO_QUERY
);
99 xRet
= xDataFactory
->createDataDescriptor();
101 xRet
= new ::connectivity::sdbcx::OView(isCaseSensitive(),m_xMetaData
);
107 ObjectType
OViewContainer::appendObject( const OUString
& _rForName
, const Reference
< XPropertySet
>& descriptor
)
109 // append the new table with a create stmt
110 OUString aName
= getString(descriptor
->getPropertyValue(PROPERTY_NAME
));
112 Reference
<XAppend
> xAppend(m_xMasterContainer
,UNO_QUERY
);
113 Reference
< XPropertySet
> xProp
= descriptor
;
116 EnsureReset
aReset(m_nInAppend
);
118 xAppend
->appendByDescriptor(descriptor
);
119 if(m_xMasterContainer
->hasByName(aName
))
120 xProp
.set(m_xMasterContainer
->getByName(aName
),UNO_QUERY
);
124 OUString sComposedName
= ::dbtools::composeTableName( m_xMetaData
, descriptor
, ::dbtools::EComposeRule::InTableDefinitions
, true );
125 if(sComposedName
.isEmpty())
126 ::dbtools::throwFunctionSequenceException(static_cast<XTypeProvider
*>(static_cast<OFilteredContainer
*>(this)));
129 descriptor
->getPropertyValue(PROPERTY_COMMAND
) >>= sCommand
;
131 OUString aSQL
= "CREATE VIEW " + sComposedName
+ " AS " + sCommand
;
133 Reference
<XConnection
> xCon
= m_xConnection
;
134 OSL_ENSURE(xCon
.is(),"Connection is null!");
137 ::utl::SharedUNOComponent
< XStatement
> xStmt( xCon
->createStatement() );
139 xStmt
->execute( aSQL
);
143 return createObject( _rForName
);
147 void OViewContainer::dropObject(sal_Int32 _nPos
, const OUString
& _sElementName
)
149 if ( m_bInElementRemoved
)
152 Reference
< XDrop
> xDrop(m_xMasterContainer
,UNO_QUERY
);
154 xDrop
->dropByName(_sElementName
);
157 OUString sComposedName
;
159 Reference
<XPropertySet
> xTable(getObject(_nPos
),UNO_QUERY
);
162 OUString sCatalog
,sSchema
,sTable
;
163 xTable
->getPropertyValue(PROPERTY_CATALOGNAME
) >>= sCatalog
;
164 xTable
->getPropertyValue(PROPERTY_SCHEMANAME
) >>= sSchema
;
165 xTable
->getPropertyValue(PROPERTY_NAME
) >>= sTable
;
167 sComposedName
= ::dbtools::composeTableName( m_xMetaData
, sCatalog
, sSchema
, sTable
, true, ::dbtools::EComposeRule::InTableDefinitions
);
170 if(sComposedName
.isEmpty())
171 ::dbtools::throwFunctionSequenceException(static_cast<XTypeProvider
*>(static_cast<OFilteredContainer
*>(this)));
173 OUString aSql
= "DROP VIEW " + sComposedName
;
174 Reference
<XConnection
> xCon
= m_xConnection
;
175 OSL_ENSURE(xCon
.is(),"Connection is null!");
178 Reference
< XStatement
> xStmt
= xCon
->createStatement( );
180 xStmt
->execute(aSql
);
181 ::comphelper::disposeComponent(xStmt
);
186 void SAL_CALL
OViewContainer::elementInserted( const ContainerEvent
& Event
)
188 ::osl::MutexGuard
aGuard(m_rMutex
);
190 if ( ( Event
.Accessor
>>= sName
)
192 && ( !hasByName( sName
) )
195 Reference
<XPropertySet
> xProp(Event
.Element
,UNO_QUERY
);
197 xProp
->getPropertyValue(PROPERTY_TYPE
) >>= sType
;
198 if ( sType
== "VIEW" )
199 insertElement(sName
,createObject(sName
));
203 void SAL_CALL
OViewContainer::elementRemoved( const ContainerEvent
& Event
)
205 ::osl::MutexGuard
aGuard(m_rMutex
);
207 if ( !((Event
.Accessor
>>= sName
) && hasByName(sName
)) )
210 m_bInElementRemoved
= true;
217 m_bInElementRemoved
= false;
220 m_bInElementRemoved
= false;
223 void SAL_CALL
OViewContainer::disposing( const css::lang::EventObject
& /*Source*/ )
227 void SAL_CALL
OViewContainer::elementReplaced( const ContainerEvent
& /*Event*/ )
231 OUString
OViewContainer::getTableTypeRestriction() const
233 // no restriction at all (other than the ones provided externally)
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */