1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: HViews.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
34 #include "hsqldb/HTables.hxx"
35 #include "hsqldb/HViews.hxx"
36 #include "hsqldb/HView.hxx"
37 #include <com/sun/star/sdbc/XRow.hpp>
38 #include <com/sun/star/sdbc/XResultSet.hpp>
39 #include <com/sun/star/sdbc/ColumnValue.hpp>
40 #include <com/sun/star/sdbc/KeyRule.hpp>
41 #include <com/sun/star/sdbcx/KeyType.hpp>
42 #include <com/sun/star/sdbcx/CheckOption.hpp>
43 #include "hsqldb/HCatalog.hxx"
44 #include <comphelper/extract.hxx>
45 #include "connectivity/dbtools.hxx"
46 #include "connectivity/dbexception.hxx"
47 #include <cppuhelper/interfacecontainer.h>
48 #include <comphelper/types.hxx>
49 #include "TConnection.hxx"
51 using namespace ::comphelper
;
53 using namespace ::cppu
;
54 using namespace connectivity
;
55 using namespace connectivity::hsqldb
;
56 using namespace ::com::sun::star::uno
;
57 using namespace ::com::sun::star::beans
;
58 using namespace ::com::sun::star::sdbcx
;
59 using namespace ::com::sun::star::sdbc
;
60 using namespace ::com::sun::star::container
;
61 using namespace ::com::sun::star::lang
;
62 using namespace dbtools
;
63 typedef connectivity::sdbcx::OCollection OCollection_TYPE
;
65 // -------------------------------------------------------------------------
66 HViews::HViews( const Reference
< XConnection
>& _rxConnection
, ::cppu::OWeakObject
& _rParent
, ::osl::Mutex
& _rMutex
,
67 const TStringVector
&_rVector
)
68 :sdbcx::OCollection( _rParent
, sal_True
, _rMutex
, _rVector
)
69 ,m_xConnection( _rxConnection
)
70 ,m_xMetaData( _rxConnection
->getMetaData() )
71 ,m_bInDrop( sal_False
)
75 // -------------------------------------------------------------------------
76 sdbcx::ObjectType
HViews::createObject(const ::rtl::OUString
& _rName
)
78 ::rtl::OUString sCatalog
,sSchema
,sTable
;
79 ::dbtools::qualifiedNameComponents(m_xMetaData
,
84 ::dbtools::eInDataManipulation
);
85 return new HView( m_xConnection
, isCaseSensitive(), sSchema
, sTable
);
88 // -------------------------------------------------------------------------
89 void HViews::impl_refresh( ) throw(RuntimeException
)
91 static_cast<OHCatalog
&>(m_rParent
).refreshTables();
93 // -------------------------------------------------------------------------
94 void HViews::disposing(void)
97 OCollection::disposing();
99 // -------------------------------------------------------------------------
100 Reference
< XPropertySet
> HViews::createDescriptor()
102 Reference
<XConnection
> xConnection
= static_cast<OHCatalog
&>(m_rParent
).getConnection();
103 connectivity::sdbcx::OView
* pNew
= new connectivity::sdbcx::OView(sal_True
,xConnection
->getMetaData());
106 // -------------------------------------------------------------------------
108 sdbcx::ObjectType
HViews::appendObject( const ::rtl::OUString
& _rForName
, const Reference
< XPropertySet
>& descriptor
)
110 createView(descriptor
);
111 return createObject( _rForName
);
113 // -------------------------------------------------------------------------
115 void HViews::dropObject(sal_Int32 _nPos
,const ::rtl::OUString
/*_sElementName*/)
120 Reference
< XInterface
> xObject( getObject( _nPos
) );
121 sal_Bool bIsNew
= connectivity::sdbcx::ODescriptor::isNew( xObject
);
124 ::rtl::OUString aSql
= ::rtl::OUString::createFromAscii("DROP VIEW");
126 Reference
<XPropertySet
> xProp(xObject
,UNO_QUERY
);
127 aSql
+= ::dbtools::composeTableName( m_xMetaData
, xProp
, ::dbtools::eInTableDefinitions
, false, false, true );
129 Reference
<XConnection
> xConnection
= static_cast<OHCatalog
&>(m_rParent
).getConnection();
130 Reference
< XStatement
> xStmt
= xConnection
->createStatement( );
131 xStmt
->execute(aSql
);
132 ::comphelper::disposeComponent(xStmt
);
135 // -----------------------------------------------------------------------------
136 void HViews::dropByNameImpl(const ::rtl::OUString
& elementName
)
138 m_bInDrop
= sal_True
;
139 OCollection_TYPE::dropByName(elementName
);
140 m_bInDrop
= sal_False
;
142 // -----------------------------------------------------------------------------
143 void HViews::createView( const Reference
< XPropertySet
>& descriptor
)
145 Reference
<XConnection
> xConnection
= static_cast<OHCatalog
&>(m_rParent
).getConnection();
147 ::rtl::OUString aSql
= ::rtl::OUString::createFromAscii("CREATE VIEW ");
148 ::rtl::OUString aQuote
= xConnection
->getMetaData()->getIdentifierQuoteString( );
149 ::rtl::OUString sSchema
,sCommand
;
151 aSql
+= ::dbtools::composeTableName( m_xMetaData
, descriptor
, ::dbtools::eInTableDefinitions
, false, false, true );
153 aSql
+= ::rtl::OUString::createFromAscii(" AS ");
154 descriptor
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_COMMAND
)) >>= sCommand
;
157 Reference
< XStatement
> xStmt
= xConnection
->createStatement( );
160 xStmt
->execute(aSql
);
161 ::comphelper::disposeComponent(xStmt
);
164 // insert the new view also in the tables collection
165 OTables
* pTables
= static_cast<OTables
*>(static_cast<OHCatalog
&>(m_rParent
).getPrivateTables());
168 ::rtl::OUString sName
= ::dbtools::composeTableName( m_xMetaData
, descriptor
, ::dbtools::eInDataManipulation
, false, false, false );
169 pTables
->appendNew(sName
);
172 // -----------------------------------------------------------------------------