bump product version to 7.2.5.1
[LibreOffice.git] / connectivity / source / drivers / mysql_jdbc / YTables.cxx
blobbe962d3d7c33301630180b9e13e4190b1468bc3b
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/sdbcx/Privilege.hpp>
26 #include <mysql/YCatalog.hxx>
27 #include <connectivity/dbtools.hxx>
28 #include <comphelper/types.hxx>
29 #include <TConnection.hxx>
31 using namespace ::comphelper;
32 using namespace connectivity;
33 using namespace ::cppu;
34 using namespace connectivity::mysql;
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star::beans;
37 using namespace ::com::sun::star::sdbcx;
38 using namespace ::com::sun::star::sdbc;
39 using namespace ::com::sun::star::container;
40 using namespace ::com::sun::star::lang;
41 using namespace dbtools;
43 sdbcx::ObjectType OTables::createObject(const OUString& _rName)
45 OUString sCatalog, sSchema, sTable;
46 ::dbtools::qualifiedNameComponents(m_xMetaData, _rName, sCatalog, sSchema, sTable,
47 ::dbtools::EComposeRule::InDataManipulation);
49 Sequence<OUString> sTableTypes{
50 "VIEW", "TABLE", "%"
51 }; // this last one just to be sure to include anything else...
53 Any aCatalog;
54 if (!sCatalog.isEmpty())
55 aCatalog <<= sCatalog;
56 Reference<XResultSet> xResult = m_xMetaData->getTables(aCatalog, sSchema, sTable, sTableTypes);
58 sdbcx::ObjectType xRet;
59 if (xResult.is())
61 Reference<XRow> xRow(xResult, UNO_QUERY);
62 if (xResult->next()) // there can be only one table with this name
64 sal_Int32 const nPrivileges = Privilege::DROP | Privilege::REFERENCE | Privilege::ALTER
65 | Privilege::CREATE | Privilege::READ | Privilege::DELETE
66 | Privilege::UPDATE | Privilege::INSERT
67 | Privilege::SELECT;
69 xRet = new OMySQLTable(this, static_cast<OMySQLCatalog&>(m_rParent).getConnection(),
70 sTable, xRow->getString(4), xRow->getString(5), sSchema,
71 sCatalog, nPrivileges);
73 ::comphelper::disposeComponent(xResult);
76 return xRet;
79 void OTables::impl_refresh() { static_cast<OMySQLCatalog&>(m_rParent).refreshTables(); }
81 void OTables::disposing()
83 m_xMetaData.clear();
84 OCollection::disposing();
87 Reference<XPropertySet> OTables::createDescriptor()
89 return new OMySQLTable(this, static_cast<OMySQLCatalog&>(m_rParent).getConnection());
92 // XAppend
93 sdbcx::ObjectType OTables::appendObject(const OUString& _rForName,
94 const Reference<XPropertySet>& descriptor)
96 createTable(descriptor);
97 return createObject(_rForName);
100 // XDrop
101 void OTables::dropObject(sal_Int32 _nPos, const OUString& _sElementName)
103 Reference<XInterface> xObject(getObject(_nPos));
104 bool bIsNew = connectivity::sdbcx::ODescriptor::isNew(xObject);
105 if (bIsNew)
106 return;
108 Reference<XConnection> xConnection = static_cast<OMySQLCatalog&>(m_rParent).getConnection();
110 OUString sCatalog, sSchema, sTable;
111 ::dbtools::qualifiedNameComponents(m_xMetaData, _sElementName, sCatalog, sSchema, sTable,
112 ::dbtools::EComposeRule::InDataManipulation);
114 OUString aSql("DROP ");
116 Reference<XPropertySet> xProp(xObject, UNO_QUERY);
117 bool bIsView = xProp.is()
118 && ::comphelper::getString(xProp->getPropertyValue(
119 OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE)))
120 == "VIEW";
121 if (bIsView) // here we have a view
122 aSql += "VIEW ";
123 else
124 aSql += "TABLE ";
126 OUString sComposedName(::dbtools::composeTableName(
127 m_xMetaData, sCatalog, sSchema, sTable, true, ::dbtools::EComposeRule::InDataManipulation));
128 aSql += sComposedName;
129 Reference<XStatement> xStmt = xConnection->createStatement();
130 if (xStmt.is())
132 xStmt->execute(aSql);
133 ::comphelper::disposeComponent(xStmt);
135 // if no exception was thrown we must delete it from the views
136 if (bIsView)
138 OViews* pViews
139 = static_cast<OViews*>(static_cast<OMySQLCatalog&>(m_rParent).getPrivateViews());
140 if (pViews && pViews->hasByName(_sElementName))
141 pViews->dropByNameImpl(_sElementName);
145 OUString OTables::adjustSQL(const OUString& _sSql)
147 OUString sSQL = _sSql;
148 static const char s_sUNSIGNED[] = "UNSIGNED";
149 sal_Int32 nIndex = sSQL.indexOf(s_sUNSIGNED);
150 while (nIndex != -1)
152 sal_Int32 nParen = sSQL.indexOf(')', nIndex);
153 sal_Int32 nPos = nIndex + strlen(s_sUNSIGNED);
154 OUString sNewUnsigned(sSQL.copy(nPos, nParen - nPos + 1));
155 sSQL = sSQL.replaceAt(nIndex, strlen(s_sUNSIGNED) + sNewUnsigned.getLength(),
156 sNewUnsigned + s_sUNSIGNED);
157 nIndex = sSQL.indexOf(s_sUNSIGNED, nIndex + strlen(s_sUNSIGNED) + sNewUnsigned.getLength());
159 return sSQL;
162 void OTables::createTable(const Reference<XPropertySet>& descriptor)
164 const Reference<XConnection> xConnection
165 = static_cast<OMySQLCatalog&>(m_rParent).getConnection();
166 const OUString aSql
167 = adjustSQL(::dbtools::createSqlCreateTableStatement(descriptor, xConnection));
168 Reference<XStatement> xStmt = xConnection->createStatement();
169 if (xStmt.is())
171 xStmt->execute(aSql);
172 ::comphelper::disposeComponent(xStmt);
176 void OTables::appendNew(const OUString& _rsNewTable)
178 insertElement(_rsNewTable, nullptr);
180 // notify our container listeners
181 ContainerEvent aEvent(static_cast<XContainer*>(this), makeAny(_rsNewTable), Any(), Any());
182 OInterfaceIteratorHelper2 aListenerLoop(m_aContainerListeners);
183 while (aListenerLoop.hasMoreElements())
184 static_cast<XContainerListener*>(aListenerLoop.next())->elementInserted(aEvent);
187 OUString OTables::getNameForObject(const sdbcx::ObjectType& _xObject)
189 OSL_ENSURE(_xObject.is(), "OTables::getNameForObject: Object is NULL!");
190 return ::dbtools::composeTableName(m_xMetaData, _xObject,
191 ::dbtools::EComposeRule::InDataManipulation, false);
194 void OTables::addComment(const Reference<XPropertySet>& descriptor, OUStringBuffer& _rOut)
196 OUString sDesc;
197 descriptor->getPropertyValue(
198 OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DESCRIPTION))
199 >>= sDesc;
200 if (!sDesc.isEmpty())
202 _rOut.append(" COMMENT '");
203 _rOut.append(sDesc);
204 _rOut.append("'");
208 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */