xref: /trunk/main/connectivity/source/drivers/mysql/YViews.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_connectivity.hxx"
30 #include "mysql/YViews.hxx"
31 #include "mysql/YTables.hxx"
32 #include <com/sun/star/sdbc/XRow.hpp>
33 #include <com/sun/star/sdbc/XResultSet.hpp>
34 #include <com/sun/star/sdbc/ColumnValue.hpp>
35 #include <com/sun/star/sdbc/KeyRule.hpp>
36 #include <com/sun/star/sdbcx/KeyType.hpp>
37 #include <com/sun/star/sdbcx/CheckOption.hpp>
38 #include "mysql/YCatalog.hxx"
39 #include <comphelper/extract.hxx>
40 #include "connectivity/dbtools.hxx"
41 #include "connectivity/dbexception.hxx"
42 #include <cppuhelper/interfacecontainer.h>
43 #include "connectivity/sdbcx/VView.hxx"
44 #include <comphelper/types.hxx>
45 #include "TConnection.hxx"
46 
47 using namespace ::comphelper;
48 
49 using namespace ::cppu;
50 using namespace connectivity;
51 using namespace connectivity::mysql;
52 using namespace ::com::sun::star::uno;
53 using namespace ::com::sun::star::beans;
54 using namespace ::com::sun::star::sdbcx;
55 using namespace ::com::sun::star::sdbc;
56 using namespace ::com::sun::star::container;
57 using namespace ::com::sun::star::lang;
58 using namespace dbtools;
59 typedef connectivity::sdbcx::OCollection OCollection_TYPE;
60 
61 sdbcx::ObjectType OViews::createObject(const ::rtl::OUString& _rName)
62 {
63     ::rtl::OUString sCatalog,sSchema,sTable;
64     ::dbtools::qualifiedNameComponents(m_xMetaData,
65                                         _rName,
66                                         sCatalog,
67                                         sSchema,
68                                         sTable,
69                                         ::dbtools::eInDataManipulation);
70     return new ::connectivity::sdbcx::OView(isCaseSensitive(),
71                             sTable,
72                             m_xMetaData,
73                             0,
74                             ::rtl::OUString(),
75                             sSchema,
76                             sCatalog
77                             );
78 }
79 // -------------------------------------------------------------------------
80 void OViews::impl_refresh(  ) throw(RuntimeException)
81 {
82     static_cast<OMySQLCatalog&>(m_rParent).refreshTables();
83 }
84 // -------------------------------------------------------------------------
85 void OViews::disposing(void)
86 {
87 m_xMetaData.clear();
88     OCollection::disposing();
89 }
90 // -------------------------------------------------------------------------
91 Reference< XPropertySet > OViews::createDescriptor()
92 {
93     Reference<XConnection> xConnection = static_cast<OMySQLCatalog&>(m_rParent).getConnection();
94     connectivity::sdbcx::OView* pNew = new connectivity::sdbcx::OView(sal_True,xConnection->getMetaData());
95     return pNew;
96 }
97 // -------------------------------------------------------------------------
98 // XAppend
99 sdbcx::ObjectType OViews::appendObject( const ::rtl::OUString& _rForName, const Reference< XPropertySet >& descriptor )
100 {
101     createView(descriptor);
102     return createObject( _rForName );
103 }
104 // -------------------------------------------------------------------------
105 // XDrop
106 void OViews::dropObject(sal_Int32 _nPos,const ::rtl::OUString /*_sElementName*/)
107 {
108     if ( m_bInDrop )
109         return;
110 
111     Reference< XInterface > xObject( getObject( _nPos ) );
112     sal_Bool bIsNew = connectivity::sdbcx::ODescriptor::isNew( xObject );
113     if (!bIsNew)
114     {
115         ::rtl::OUString aSql = ::rtl::OUString::createFromAscii("DROP VIEW");
116 
117         Reference<XPropertySet> xProp(xObject,UNO_QUERY);
118         aSql += ::dbtools::composeTableName( m_xMetaData, xProp, ::dbtools::eInTableDefinitions, false, false, true );
119 
120         Reference<XConnection> xConnection = static_cast<OMySQLCatalog&>(m_rParent).getConnection();
121         Reference< XStatement > xStmt = xConnection->createStatement(  );
122         xStmt->execute(aSql);
123         ::comphelper::disposeComponent(xStmt);
124     }
125 }
126 // -----------------------------------------------------------------------------
127 void OViews::dropByNameImpl(const ::rtl::OUString& elementName)
128 {
129     m_bInDrop = sal_True;
130     OCollection_TYPE::dropByName(elementName);
131     m_bInDrop = sal_False;
132 }
133 // -----------------------------------------------------------------------------
134 void OViews::createView( const Reference< XPropertySet >& descriptor )
135 {
136     Reference<XConnection> xConnection = static_cast<OMySQLCatalog&>(m_rParent).getConnection();
137 
138     ::rtl::OUString aSql    = ::rtl::OUString::createFromAscii("CREATE VIEW ");
139     ::rtl::OUString aQuote  = xConnection->getMetaData()->getIdentifierQuoteString(  );
140     ::rtl::OUString sSchema,sCommand;
141 
142     aSql += ::dbtools::composeTableName( m_xMetaData, descriptor, ::dbtools::eInTableDefinitions, false, false, true );
143 
144     aSql += ::rtl::OUString::createFromAscii(" AS ");
145     descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_COMMAND)) >>= sCommand;
146     aSql += sCommand;
147 
148     Reference< XStatement > xStmt = xConnection->createStatement(  );
149     if ( xStmt.is() )
150     {
151         xStmt->execute(aSql);
152         ::comphelper::disposeComponent(xStmt);
153     }
154 
155     // insert the new view also in the tables collection
156     OTables* pTables = static_cast<OTables*>(static_cast<OMySQLCatalog&>(m_rParent).getPrivateTables());
157     if ( pTables )
158     {
159         ::rtl::OUString sName = ::dbtools::composeTableName( m_xMetaData, descriptor, ::dbtools::eInDataManipulation, false, false, false );
160         pTables->appendNew(sName);
161     }
162 }
163