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 <strings.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::sdbcx
;
38 using namespace ::com::sun::star::container
;
39 using namespace ::osl
;
40 using namespace ::comphelper
;
41 using namespace ::cppu
;
42 using namespace ::connectivity::sdbcx
;
46 OViewContainer::OViewContainer(::cppu::OWeakObject
& _rParent
47 ,::osl::Mutex
& _rMutex
48 ,const Reference
< XConnection
>& _xCon
50 ,IRefreshListener
* _pRefreshListener
51 ,std::atomic
<std::size_t>& _nInAppend
)
52 :OFilteredContainer(_rParent
,_rMutex
,_xCon
,_bCase
,_pRefreshListener
,_nInAppend
)
53 ,m_bInElementRemoved(false)
57 OViewContainer::~OViewContainer()
62 OUString SAL_CALL
OViewContainer::getImplementationName()
64 return u
"com.sun.star.sdb.dbaccess.OViewContainer"_ustr
;
66 sal_Bool SAL_CALL
OViewContainer::supportsService(const OUString
& _rServiceName
)
68 const css::uno::Sequence
< OUString
> aSupported(getSupportedServiceNames());
69 for (const OUString
& s
: aSupported
)
70 if (s
== _rServiceName
)
75 css::uno::Sequence
< OUString
> SAL_CALL
OViewContainer::getSupportedServiceNames()
77 return { SERVICE_SDBCX_CONTAINER
, SERVICE_SDBCX_TABLES
};
81 ObjectType
OViewContainer::createObject(const OUString
& _rName
)
84 if ( m_xMasterContainer
.is() && m_xMasterContainer
->hasByName(_rName
) )
85 xProp
.set(m_xMasterContainer
->getByName(_rName
),UNO_QUERY
);
89 OUString sCatalog
,sSchema
,sTable
;
90 ::dbtools::qualifiedNameComponents(m_xMetaData
,
95 ::dbtools::EComposeRule::InDataManipulation
);
96 return new View(m_xConnection
,
107 Reference
< XPropertySet
> OViewContainer::createDescriptor()
109 Reference
< XPropertySet
> xRet
;
110 // first we have to look if the master tables support this
111 // and if so then create a table object as well with the master tables
112 Reference
<XDataDescriptorFactory
> xDataFactory(m_xMasterContainer
,UNO_QUERY
);
113 if(xDataFactory
.is())
114 xRet
= xDataFactory
->createDataDescriptor();
116 xRet
= new ::connectivity::sdbcx::OView(isCaseSensitive(),m_xMetaData
);
122 ObjectType
OViewContainer::appendObject( const OUString
& _rForName
, const Reference
< XPropertySet
>& descriptor
)
124 // append the new table with a create stmt
125 OUString aName
= getString(descriptor
->getPropertyValue(PROPERTY_NAME
));
127 Reference
<XAppend
> xAppend(m_xMasterContainer
,UNO_QUERY
);
128 Reference
< XPropertySet
> xProp
= descriptor
;
131 EnsureReset
aReset(m_nInAppend
);
133 xAppend
->appendByDescriptor(descriptor
);
134 if(m_xMasterContainer
->hasByName(aName
))
135 xProp
.set(m_xMasterContainer
->getByName(aName
),UNO_QUERY
);
139 OUString sComposedName
= ::dbtools::composeTableName( m_xMetaData
, descriptor
, ::dbtools::EComposeRule::InTableDefinitions
, true );
140 if(sComposedName
.isEmpty())
141 ::dbtools::throwFunctionSequenceException(static_cast<XTypeProvider
*>(static_cast<OFilteredContainer
*>(this)));
144 descriptor
->getPropertyValue(PROPERTY_COMMAND
) >>= sCommand
;
146 OUString aSQL
= "CREATE VIEW " + sComposedName
+ " AS " + sCommand
;
148 Reference
<XConnection
> xCon
= m_xConnection
;
149 OSL_ENSURE(xCon
.is(),"Connection is null!");
152 ::utl::SharedUNOComponent
< XStatement
> xStmt( xCon
->createStatement() );
154 xStmt
->execute( aSQL
);
158 return createObject( _rForName
);
162 void OViewContainer::dropObject(sal_Int32 _nPos
, const OUString
& _sElementName
)
164 if ( m_bInElementRemoved
)
167 Reference
< XDrop
> xDrop(m_xMasterContainer
,UNO_QUERY
);
169 xDrop
->dropByName(_sElementName
);
172 OUString sComposedName
;
174 Reference
<XPropertySet
> xTable(getObject(_nPos
),UNO_QUERY
);
177 OUString sCatalog
,sSchema
,sTable
;
178 xTable
->getPropertyValue(PROPERTY_CATALOGNAME
) >>= sCatalog
;
179 xTable
->getPropertyValue(PROPERTY_SCHEMANAME
) >>= sSchema
;
180 xTable
->getPropertyValue(PROPERTY_NAME
) >>= sTable
;
182 sComposedName
= ::dbtools::composeTableName( m_xMetaData
, sCatalog
, sSchema
, sTable
, true, ::dbtools::EComposeRule::InTableDefinitions
);
185 if(sComposedName
.isEmpty())
186 ::dbtools::throwFunctionSequenceException(static_cast<XTypeProvider
*>(static_cast<OFilteredContainer
*>(this)));
188 OUString aSql
= "DROP VIEW " + sComposedName
;
189 Reference
<XConnection
> xCon
= m_xConnection
;
190 OSL_ENSURE(xCon
.is(),"Connection is null!");
193 Reference
< XStatement
> xStmt
= xCon
->createStatement( );
195 xStmt
->execute(aSql
);
196 ::comphelper::disposeComponent(xStmt
);
201 void SAL_CALL
OViewContainer::elementInserted( const ContainerEvent
& Event
)
203 ::osl::MutexGuard
aGuard(m_rMutex
);
205 if ( ( Event
.Accessor
>>= sName
)
207 && ( !hasByName( sName
) )
210 Reference
<XPropertySet
> xProp(Event
.Element
,UNO_QUERY
);
212 xProp
->getPropertyValue(PROPERTY_TYPE
) >>= sType
;
213 if ( sType
== "VIEW" )
214 insertElement(sName
,createObject(sName
));
218 void SAL_CALL
OViewContainer::elementRemoved( const ContainerEvent
& Event
)
220 ::osl::MutexGuard
aGuard(m_rMutex
);
222 if ( !((Event
.Accessor
>>= sName
) && hasByName(sName
)) )
225 m_bInElementRemoved
= true;
232 m_bInElementRemoved
= false;
235 m_bInElementRemoved
= false;
238 void SAL_CALL
OViewContainer::disposing( const css::lang::EventObject
& /*Source*/ )
242 void SAL_CALL
OViewContainer::elementReplaced( const ContainerEvent
& /*Event*/ )
246 OUString
OViewContainer::getTableTypeRestriction() const
248 // no restriction at all (other than the ones provided externally)
252 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */