build fix
[LibreOffice.git] / connectivity / source / drivers / mysql / YTables.cxx
blob3cad5360c033a5f617f4d988081ec3a08c363b13
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::EComposeRule::InDataManipulation);
54 Sequence< OUString > sTableTypes(3);
55 sTableTypes[0] = "VIEW";
56 sTableTypes[1] = "TABLE";
57 sTableTypes[2] = "%"; // just to be sure to include anything else ....
59 Any aCatalog;
60 if ( !sCatalog.isEmpty() )
61 aCatalog <<= sCatalog;
62 Reference< XResultSet > xResult = m_xMetaData->getTables(aCatalog,sSchema,sTable,sTableTypes);
64 sdbcx::ObjectType xRet = nullptr;
65 if ( xResult.is() )
67 Reference< XRow > xRow(xResult,UNO_QUERY);
68 if ( xResult->next() ) // there can be only one table with this name
70 sal_Int32 nPrivileges = Privilege::DROP |
71 Privilege::REFERENCE |
72 Privilege::ALTER |
73 Privilege::CREATE |
74 Privilege::READ |
75 Privilege::DELETE |
76 Privilege::UPDATE |
77 Privilege::INSERT |
78 Privilege::SELECT;
80 OMySQLTable* pRet = new OMySQLTable( this
81 ,static_cast<OMySQLCatalog&>(m_rParent).getConnection()
82 ,sTable
83 ,xRow->getString(4)
84 ,xRow->getString(5)
85 ,sSchema
86 ,sCatalog
87 ,nPrivileges);
88 xRet = pRet;
90 ::comphelper::disposeComponent(xResult);
93 return xRet;
96 void OTables::impl_refresh( ) throw(RuntimeException)
98 static_cast<OMySQLCatalog&>(m_rParent).refreshTables();
101 void OTables::disposing()
103 m_xMetaData.clear();
104 OCollection::disposing();
107 Reference< XPropertySet > OTables::createDescriptor()
109 return new OMySQLTable(this,static_cast<OMySQLCatalog&>(m_rParent).getConnection());
112 // XAppend
113 sdbcx::ObjectType OTables::appendObject( const OUString& _rForName, const Reference< XPropertySet >& descriptor )
115 createTable(descriptor);
116 return createObject( _rForName );
119 // XDrop
120 void OTables::dropObject(sal_Int32 _nPos,const OUString& _sElementName)
122 Reference< XInterface > xObject( getObject( _nPos ) );
123 bool bIsNew = connectivity::sdbcx::ODescriptor::isNew( xObject );
124 if (!bIsNew)
126 Reference< XConnection > xConnection = static_cast<OMySQLCatalog&>(m_rParent).getConnection();
129 OUString sCatalog,sSchema,sTable;
130 ::dbtools::qualifiedNameComponents(m_xMetaData,_sElementName,sCatalog,sSchema,sTable,::dbtools::EComposeRule::InDataManipulation);
132 OUString aSql( "DROP " );
134 Reference<XPropertySet> xProp(xObject,UNO_QUERY);
135 bool bIsView = xProp.is() && ::comphelper::getString(xProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE))) == "VIEW";
136 if(bIsView) // here we have a view
137 aSql += "VIEW ";
138 else
139 aSql += "TABLE ";
141 OUString sComposedName(
142 ::dbtools::composeTableName( m_xMetaData, sCatalog, sSchema, sTable, true, ::dbtools::EComposeRule::InDataManipulation ) );
143 aSql += sComposedName;
144 Reference< XStatement > xStmt = xConnection->createStatement( );
145 if ( xStmt.is() )
147 xStmt->execute(aSql);
148 ::comphelper::disposeComponent(xStmt);
150 // if no exception was thrown we must delete it from the views
151 if ( bIsView )
153 OViews* pViews = static_cast<OViews*>(static_cast<OMySQLCatalog&>(m_rParent).getPrivateViews());
154 if ( pViews && pViews->hasByName(_sElementName) )
155 pViews->dropByNameImpl(_sElementName);
160 OUString OTables::adjustSQL(const OUString& _sSql)
162 OUString sSQL = _sSql;
163 static const char s_sUNSIGNED[] = "UNSIGNED";
164 sal_Int32 nIndex = sSQL.indexOf(s_sUNSIGNED);
165 while(nIndex != -1 )
167 sal_Int32 nParen = sSQL.indexOf(')',nIndex);
168 sal_Int32 nPos = nIndex + strlen(s_sUNSIGNED);
169 OUString sNewUnsigned( sSQL.copy(nPos,nParen - nPos + 1));
170 sSQL = sSQL.replaceAt(nIndex, strlen(s_sUNSIGNED) + sNewUnsigned.getLength(), sNewUnsigned + s_sUNSIGNED);
171 nIndex = sSQL.indexOf(s_sUNSIGNED,nIndex + strlen(s_sUNSIGNED) + sNewUnsigned.getLength());
173 return sSQL;
176 void OTables::createTable( const Reference< XPropertySet >& descriptor )
178 const Reference< XConnection > xConnection = static_cast<OMySQLCatalog&>(m_rParent).getConnection();
179 const OUString aSql = adjustSQL(::dbtools::createSqlCreateTableStatement(descriptor,xConnection, this, "(M,D)"));
180 Reference< XStatement > xStmt = xConnection->createStatement( );
181 if ( xStmt.is() )
183 xStmt->execute(aSql);
184 ::comphelper::disposeComponent(xStmt);
188 void OTables::appendNew(const OUString& _rsNewTable)
190 insertElement(_rsNewTable,nullptr);
192 // notify our container listeners
193 ContainerEvent aEvent(static_cast<XContainer*>(this), makeAny(_rsNewTable), Any(), Any());
194 OInterfaceIteratorHelper2 aListenerLoop(m_aContainerListeners);
195 while (aListenerLoop.hasMoreElements())
196 static_cast<XContainerListener*>(aListenerLoop.next())->elementInserted(aEvent);
199 OUString OTables::getNameForObject(const sdbcx::ObjectType& _xObject)
201 OSL_ENSURE(_xObject.is(),"OTables::getNameForObject: Object is NULL!");
202 return ::dbtools::composeTableName( m_xMetaData, _xObject, ::dbtools::EComposeRule::InDataManipulation, false, false, false );
205 void OTables::addComment(const Reference< XPropertySet >& descriptor,OUStringBuffer& _rOut)
207 OUString sDesc;
208 descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DESCRIPTION)) >>= sDesc;
209 if ( !sDesc.isEmpty() )
211 _rOut.append(" COMMENT '");
212 _rOut.append(sDesc);
213 _rOut.append("'");
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */