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: YTables.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"
33 #include "mysql/YTables.hxx"
34 #include "mysql/YViews.hxx"
35 #include "mysql/YTable.hxx"
36 #include <com/sun/star/sdbc/XRow.hpp>
37 #include <com/sun/star/sdbc/XResultSet.hpp>
38 #include <com/sun/star/sdbc/ColumnValue.hpp>
39 #include <com/sun/star/sdbcx/Privilege.hpp>
40 #include <com/sun/star/sdbc/KeyRule.hpp>
41 #include <com/sun/star/sdbcx/KeyType.hpp>
42 #include "mysql/YCatalog.hxx"
43 #include <comphelper/extract.hxx>
44 #include "connectivity/dbtools.hxx"
45 #include "connectivity/dbexception.hxx"
46 #include <cppuhelper/interfacecontainer.h>
47 #include <comphelper/types.hxx>
48 #include "TConnection.hxx"
50 using namespace ::comphelper
;
51 using namespace connectivity
;
52 using namespace ::cppu
;
53 using namespace connectivity::mysql
;
54 using namespace ::com::sun::star::uno
;
55 using namespace ::com::sun::star::beans
;
56 using namespace ::com::sun::star::sdbcx
;
57 using namespace ::com::sun::star::sdbc
;
58 using namespace ::com::sun::star::container
;
59 using namespace ::com::sun::star::lang
;
60 using namespace dbtools
;
61 typedef connectivity::sdbcx::OCollection OCollection_TYPE
;
63 sdbcx::ObjectType
OTables::createObject(const ::rtl::OUString
& _rName
)
65 ::rtl::OUString sCatalog
,sSchema
,sTable
;
66 ::dbtools::qualifiedNameComponents(m_xMetaData
,_rName
,sCatalog
,sSchema
,sTable
,::dbtools::eInDataManipulation
);
68 static const ::rtl::OUString
s_sTableTypeView(RTL_CONSTASCII_USTRINGPARAM("VIEW"));
69 static const ::rtl::OUString
s_sTableTypeTable(RTL_CONSTASCII_USTRINGPARAM("TABLE"));
70 static const ::rtl::OUString
s_sAll(RTL_CONSTASCII_USTRINGPARAM("%"));
72 Sequence
< ::rtl::OUString
> sTableTypes(3);
73 sTableTypes
[0] = s_sTableTypeView
;
74 sTableTypes
[1] = s_sTableTypeTable
;
75 sTableTypes
[2] = s_sAll
; // just to be sure to include anything else ....
78 if ( sCatalog
.getLength() )
79 aCatalog
<<= sCatalog
;
80 Reference
< XResultSet
> xResult
= m_xMetaData
->getTables(aCatalog
,sSchema
,sTable
,sTableTypes
);
82 sdbcx::ObjectType xRet
= NULL
;
85 Reference
< XRow
> xRow(xResult
,UNO_QUERY
);
86 if ( xResult
->next() ) // there can be only one table with this name
88 // Reference<XStatement> xStmt = m_xConnection->createStatement();
91 // Reference< XResultSet > xPrivRes = xStmt->executeQuery();
92 // Reference< XRow > xPrivRow(xPrivRes,UNO_QUERY);
93 // while ( xPrivRes.is() && xPrivRes->next() )
95 // if ( xPrivRow->getString(1) )
100 sal_Int32 nPrivileges
= Privilege::DROP
|
101 Privilege::REFERENCE
|
110 OMySQLTable
* pRet
= new OMySQLTable( this
111 ,static_cast<OMySQLCatalog
&>(m_rParent
).getConnection()
120 ::comphelper::disposeComponent(xResult
);
125 // -------------------------------------------------------------------------
126 void OTables::impl_refresh( ) throw(RuntimeException
)
128 static_cast<OMySQLCatalog
&>(m_rParent
).refreshTables();
130 // -------------------------------------------------------------------------
131 void OTables::disposing(void)
134 OCollection::disposing();
136 // -------------------------------------------------------------------------
137 Reference
< XPropertySet
> OTables::createDescriptor()
139 return new OMySQLTable(this,static_cast<OMySQLCatalog
&>(m_rParent
).getConnection());
141 // -------------------------------------------------------------------------
143 sdbcx::ObjectType
OTables::appendObject( const ::rtl::OUString
& _rForName
, const Reference
< XPropertySet
>& descriptor
)
145 createTable(descriptor
);
146 return createObject( _rForName
);
148 // -------------------------------------------------------------------------
150 void OTables::dropObject(sal_Int32 _nPos
,const ::rtl::OUString _sElementName
)
152 Reference
< XInterface
> xObject( getObject( _nPos
) );
153 sal_Bool bIsNew
= connectivity::sdbcx::ODescriptor::isNew( xObject
);
156 Reference
< XConnection
> xConnection
= static_cast<OMySQLCatalog
&>(m_rParent
).getConnection();
159 ::rtl::OUString sCatalog
,sSchema
,sTable
;
160 ::dbtools::qualifiedNameComponents(m_xMetaData
,_sElementName
,sCatalog
,sSchema
,sTable
,::dbtools::eInDataManipulation
);
162 ::rtl::OUString aSql
= ::rtl::OUString::createFromAscii("DROP ");
164 Reference
<XPropertySet
> xProp(xObject
,UNO_QUERY
);
165 sal_Bool bIsView
= xProp
.is() && ::comphelper::getString(xProp
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE
))) == ::rtl::OUString::createFromAscii("VIEW");
166 if(bIsView
) // here we have a view
167 aSql
+= ::rtl::OUString::createFromAscii("VIEW ");
169 aSql
+= ::rtl::OUString::createFromAscii("TABLE ");
171 ::rtl::OUString
sComposedName(
172 ::dbtools::composeTableName( m_xMetaData
, sCatalog
, sSchema
, sTable
, sal_True
, ::dbtools::eInDataManipulation
) );
173 aSql
+= sComposedName
;
174 Reference
< XStatement
> xStmt
= xConnection
->createStatement( );
177 xStmt
->execute(aSql
);
178 ::comphelper::disposeComponent(xStmt
);
180 // if no exception was thrown we must delete it from the views
183 OViews
* pViews
= static_cast<OViews
*>(static_cast<OMySQLCatalog
&>(m_rParent
).getPrivateViews());
184 if ( pViews
&& pViews
->hasByName(_sElementName
) )
185 pViews
->dropByNameImpl(_sElementName
);
189 // -------------------------------------------------------------------------
190 void OTables::createTable( const Reference
< XPropertySet
>& descriptor
)
192 const Reference
< XConnection
> xConnection
= static_cast<OMySQLCatalog
&>(m_rParent
).getConnection();
193 static const ::rtl::OUString
s_sCreatePattern(RTL_CONSTASCII_USTRINGPARAM("(M,D)"));
194 const ::rtl::OUString aSql
= ::dbtools::createSqlCreateTableStatement(descriptor
,xConnection
,s_sCreatePattern
);
196 Reference
< XStatement
> xStmt
= xConnection
->createStatement( );
199 xStmt
->execute(aSql
);
200 ::comphelper::disposeComponent(xStmt
);
203 // -----------------------------------------------------------------------------
204 void OTables::appendNew(const ::rtl::OUString
& _rsNewTable
)
206 insertElement(_rsNewTable
,NULL
);
208 // notify our container listeners
209 ContainerEvent
aEvent(static_cast<XContainer
*>(this), makeAny(_rsNewTable
), Any(), Any());
210 OInterfaceIteratorHelper
aListenerLoop(m_aContainerListeners
);
211 while (aListenerLoop
.hasMoreElements())
212 static_cast<XContainerListener
*>(aListenerLoop
.next())->elementInserted(aEvent
);
214 // -----------------------------------------------------------------------------
215 ::rtl::OUString
OTables::getNameForObject(const sdbcx::ObjectType
& _xObject
)
217 OSL_ENSURE(_xObject
.is(),"OTables::getNameForObject: Object is NULL!");
218 return ::dbtools::composeTableName( m_xMetaData
, _xObject
, ::dbtools::eInDataManipulation
, false, false, false );
220 // -----------------------------------------------------------------------------