Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / connectivity / source / drivers / mysql / YTables.cxx
blobd346b38033a9938562a79acf050f8e37ecedde14
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "mysql/YTables.hxx"
21 #include "mysql/YViews.hxx"
22 #include "mysql/YTable.hxx"
23 #include <com/sun/star/sdbc/XRow.hpp>
24 #include <com/sun/star/sdbc/XResultSet.hpp>
25 #include <com/sun/star/sdbc/ColumnValue.hpp>
26 #include <com/sun/star/sdbcx/Privilege.hpp>
27 #include <com/sun/star/sdbc/KeyRule.hpp>
28 #include <com/sun/star/sdbcx/KeyType.hpp>
29 #include "mysql/YCatalog.hxx"
30 #include <comphelper/extract.hxx>
31 #include "connectivity/dbtools.hxx"
32 #include "connectivity/dbexception.hxx"
33 #include <cppuhelper/interfacecontainer.h>
34 #include <comphelper/types.hxx>
35 #include "TConnection.hxx"
37 using namespace ::comphelper;
38 using namespace connectivity;
39 using namespace ::cppu;
40 using namespace connectivity::mysql;
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::beans;
43 using namespace ::com::sun::star::sdbcx;
44 using namespace ::com::sun::star::sdbc;
45 using namespace ::com::sun::star::container;
46 using namespace ::com::sun::star::lang;
47 using namespace dbtools;
49 sdbcx::ObjectType OTables::createObject(const OUString& _rName)
51 OUString sCatalog,sSchema,sTable;
52 ::dbtools::qualifiedNameComponents(m_xMetaData,_rName,sCatalog,sSchema,sTable,::dbtools::eInDataManipulation);
54 static const OUString s_sTableTypeView("VIEW");
55 static const OUString s_sTableTypeTable("TABLE");
56 static const OUString s_sAll("%");
58 Sequence< OUString > sTableTypes(3);
59 sTableTypes[0] = s_sTableTypeView;
60 sTableTypes[1] = s_sTableTypeTable;
61 sTableTypes[2] = s_sAll; // just to be sure to include anything else ....
63 Any aCatalog;
64 if ( !sCatalog.isEmpty() )
65 aCatalog <<= sCatalog;
66 Reference< XResultSet > xResult = m_xMetaData->getTables(aCatalog,sSchema,sTable,sTableTypes);
68 sdbcx::ObjectType xRet = NULL;
69 if ( xResult.is() )
71 Reference< XRow > xRow(xResult,UNO_QUERY);
72 if ( xResult->next() ) // there can be only one table with this name
74 sal_Int32 nPrivileges = Privilege::DROP |
75 Privilege::REFERENCE |
76 Privilege::ALTER |
77 Privilege::CREATE |
78 Privilege::READ |
79 Privilege::DELETE |
80 Privilege::UPDATE |
81 Privilege::INSERT |
82 Privilege::SELECT;
84 OMySQLTable* pRet = new OMySQLTable( this
85 ,static_cast<OMySQLCatalog&>(m_rParent).getConnection()
86 ,sTable
87 ,xRow->getString(4)
88 ,xRow->getString(5)
89 ,sSchema
90 ,sCatalog
91 ,nPrivileges);
92 xRet = pRet;
94 ::comphelper::disposeComponent(xResult);
97 return xRet;
100 void OTables::impl_refresh( ) throw(RuntimeException)
102 static_cast<OMySQLCatalog&>(m_rParent).refreshTables();
105 void OTables::disposing(void)
107 m_xMetaData.clear();
108 OCollection::disposing();
111 Reference< XPropertySet > OTables::createDescriptor()
113 return new OMySQLTable(this,static_cast<OMySQLCatalog&>(m_rParent).getConnection());
116 // XAppend
117 sdbcx::ObjectType OTables::appendObject( const OUString& _rForName, const Reference< XPropertySet >& descriptor )
119 createTable(descriptor);
120 return createObject( _rForName );
123 // XDrop
124 void OTables::dropObject(sal_Int32 _nPos,const OUString& _sElementName)
126 Reference< XInterface > xObject( getObject( _nPos ) );
127 bool bIsNew = connectivity::sdbcx::ODescriptor::isNew( xObject );
128 if (!bIsNew)
130 Reference< XConnection > xConnection = static_cast<OMySQLCatalog&>(m_rParent).getConnection();
133 OUString sCatalog,sSchema,sTable;
134 ::dbtools::qualifiedNameComponents(m_xMetaData,_sElementName,sCatalog,sSchema,sTable,::dbtools::eInDataManipulation);
136 OUString aSql( "DROP " );
138 Reference<XPropertySet> xProp(xObject,UNO_QUERY);
139 bool bIsView = xProp.is() && ::comphelper::getString(xProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE))) == "VIEW";
140 if(bIsView) // here we have a view
141 aSql += "VIEW ";
142 else
143 aSql += "TABLE ";
145 OUString sComposedName(
146 ::dbtools::composeTableName( m_xMetaData, sCatalog, sSchema, sTable, true, ::dbtools::eInDataManipulation ) );
147 aSql += sComposedName;
148 Reference< XStatement > xStmt = xConnection->createStatement( );
149 if ( xStmt.is() )
151 xStmt->execute(aSql);
152 ::comphelper::disposeComponent(xStmt);
154 // if no exception was thrown we must delete it from the views
155 if ( bIsView )
157 OViews* pViews = static_cast<OViews*>(static_cast<OMySQLCatalog&>(m_rParent).getPrivateViews());
158 if ( pViews && pViews->hasByName(_sElementName) )
159 pViews->dropByNameImpl(_sElementName);
164 OUString OTables::adjustSQL(const OUString& _sSql)
166 OUString sSQL = _sSql;
167 static const OUString s_sUNSIGNED("UNSIGNED");
168 sal_Int32 nIndex = sSQL.indexOf(s_sUNSIGNED);
169 while(nIndex != -1 )
171 sal_Int32 nParen = sSQL.indexOf(')',nIndex);
172 sal_Int32 nPos = nIndex + s_sUNSIGNED.getLength();
173 OUString sNewUnsigned( sSQL.copy(nPos,nParen - nPos + 1));
174 sSQL = sSQL.replaceAt(nIndex,s_sUNSIGNED.getLength()+sNewUnsigned.getLength(),sNewUnsigned + s_sUNSIGNED);
175 nIndex = sSQL.indexOf(s_sUNSIGNED,nIndex + s_sUNSIGNED.getLength()+sNewUnsigned.getLength());
177 return sSQL;
180 void OTables::createTable( const Reference< XPropertySet >& descriptor )
182 const Reference< XConnection > xConnection = static_cast<OMySQLCatalog&>(m_rParent).getConnection();
183 static const OUString s_sCreatePattern("(M,D)");
184 const OUString aSql = adjustSQL(::dbtools::createSqlCreateTableStatement(descriptor,xConnection,this,s_sCreatePattern));
185 Reference< XStatement > xStmt = xConnection->createStatement( );
186 if ( xStmt.is() )
188 xStmt->execute(aSql);
189 ::comphelper::disposeComponent(xStmt);
193 void OTables::appendNew(const OUString& _rsNewTable)
195 insertElement(_rsNewTable,NULL);
197 // notify our container listeners
198 ContainerEvent aEvent(static_cast<XContainer*>(this), makeAny(_rsNewTable), Any(), Any());
199 OInterfaceIteratorHelper aListenerLoop(m_aContainerListeners);
200 while (aListenerLoop.hasMoreElements())
201 static_cast<XContainerListener*>(aListenerLoop.next())->elementInserted(aEvent);
204 OUString OTables::getNameForObject(const sdbcx::ObjectType& _xObject)
206 OSL_ENSURE(_xObject.is(),"OTables::getNameForObject: Object is NULL!");
207 return ::dbtools::composeTableName( m_xMetaData, _xObject, ::dbtools::eInDataManipulation, false, false, false );
210 void OTables::addComment(const Reference< XPropertySet >& descriptor,OUStringBuffer& _rOut)
212 OUString sDesc;
213 descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DESCRIPTION)) >>= sDesc;
214 if ( !sDesc.isEmpty() )
216 _rOut.appendAscii(" COMMENT '");
217 _rOut.append(sDesc);
218 _rOut.appendAscii("'");
222 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */