LibreOffice Module connectivity (master) 1
YTables.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
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>
28#include <comphelper/types.hxx>
29#include <TConnection.hxx>
30
31using namespace ::comphelper;
32using namespace connectivity;
33using namespace ::cppu;
34using namespace connectivity::mysql;
35using namespace ::com::sun::star::uno;
36using namespace ::com::sun::star::beans;
37using namespace ::com::sun::star::sdbcx;
38using namespace ::com::sun::star::sdbc;
39using namespace ::com::sun::star::container;
40using namespace ::com::sun::star::lang;
41using namespace dbtools;
42
43sdbcx::ObjectType OTables::createObject(const OUString& _rName)
44{
45 OUString sCatalog, sSchema, sTable;
46 ::dbtools::qualifiedNameComponents(m_xMetaData, _rName, sCatalog, sSchema, sTable,
47 ::dbtools::EComposeRule::InDataManipulation);
48
49 Sequence<OUString> sTableTypes{
50 "VIEW", "TABLE", "%"
51 }; // this last one just to be sure to include anything else...
52
53 Any aCatalog;
54 if (!sCatalog.isEmpty())
55 aCatalog <<= sCatalog;
56 Reference<XResultSet> xResult = m_xMetaData->getTables(aCatalog, sSchema, sTable, sTableTypes);
57
59 if (xResult.is())
60 {
61 Reference<XRow> xRow(xResult, UNO_QUERY);
62 if (xResult->next()) // there can be only one table with this name
63 {
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;
68
69 xRet = new OMySQLTable(this, static_cast<OMySQLCatalog&>(m_rParent).getConnection(),
70 sTable, xRow->getString(4), xRow->getString(5), sSchema,
71 sCatalog, nPrivileges);
72 }
73 ::comphelper::disposeComponent(xResult);
74 }
75
76 return xRet;
77}
78
79void OTables::impl_refresh() { static_cast<OMySQLCatalog&>(m_rParent).refreshTables(); }
80
82{
83 m_xMetaData.clear();
85}
86
87Reference<XPropertySet> OTables::createDescriptor()
88{
89 return new OMySQLTable(this, static_cast<OMySQLCatalog&>(m_rParent).getConnection());
90}
91
92// XAppend
93sdbcx::ObjectType OTables::appendObject(const OUString& _rForName,
94 const Reference<XPropertySet>& descriptor)
95{
96 createTable(descriptor);
97 return createObject(_rForName);
98}
99
100// XDrop
101void OTables::dropObject(sal_Int32 _nPos, const OUString& _sElementName)
102{
103 Reference<XInterface> xObject(getObject(_nPos));
104 bool bIsNew = connectivity::sdbcx::ODescriptor::isNew(xObject);
105 if (bIsNew)
106 return;
107
108 Reference<XConnection> xConnection = static_cast<OMySQLCatalog&>(m_rParent).getConnection();
109
110 OUString sCatalog, sSchema, sTable;
111 ::dbtools::qualifiedNameComponents(m_xMetaData, _sElementName, sCatalog, sSchema, sTable,
112 ::dbtools::EComposeRule::InDataManipulation);
113
114 OUString aSql("DROP ");
115
116 Reference<XPropertySet> xProp(xObject, UNO_QUERY);
117 bool bIsView = xProp.is()
118 && ::comphelper::getString(xProp->getPropertyValue(
120 == "VIEW";
121 if (bIsView) // here we have a view
122 aSql += "VIEW ";
123 else
124 aSql += "TABLE ";
125
127 m_xMetaData, sCatalog, sSchema, sTable, true, ::dbtools::EComposeRule::InDataManipulation));
128 aSql += sComposedName;
129 Reference<XStatement> xStmt = xConnection->createStatement();
130 if (xStmt.is())
131 {
132 xStmt->execute(aSql);
133 ::comphelper::disposeComponent(xStmt);
134 }
135 // if no exception was thrown we must delete it from the views
136 if (bIsView)
137 {
138 OViews* pViews
139 = static_cast<OViews*>(static_cast<OMySQLCatalog&>(m_rParent).getPrivateViews());
140 if (pViews && pViews->hasByName(_sElementName))
141 pViews->dropByNameImpl(_sElementName);
142 }
143}
144
145OUString OTables::adjustSQL(const OUString& _sSql)
146{
147 OUString sSQL = _sSql;
148 static const char s_sUNSIGNED[] = "UNSIGNED";
149 sal_Int32 nIndex = sSQL.indexOf(s_sUNSIGNED);
150 while (nIndex != -1)
151 {
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 rtl::Concat2View(sNewUnsigned + s_sUNSIGNED));
157 nIndex = sSQL.indexOf(s_sUNSIGNED, nIndex + strlen(s_sUNSIGNED) + sNewUnsigned.getLength());
158 }
159 return sSQL;
160}
161
162void OTables::createTable(const Reference<XPropertySet>& descriptor)
163{
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())
170 {
171 xStmt->execute(aSql);
172 ::comphelper::disposeComponent(xStmt);
173 }
174}
175
176void OTables::appendNew(const OUString& _rsNewTable)
177{
178 insertElement(_rsNewTable, nullptr);
179
180 // notify our container listeners
181 ContainerEvent aEvent(static_cast<XContainer*>(this), Any(_rsNewTable), Any(), Any());
183 while (aListenerLoop.hasMoreElements())
184 aListenerLoop.next()->elementInserted(aEvent);
185}
186
187OUString OTables::getNameForObject(const sdbcx::ObjectType& _xObject)
188{
189 OSL_ENSURE(_xObject.is(), "OTables::getNameForObject: Object is NULL!");
191 ::dbtools::EComposeRule::InDataManipulation, false);
192}
193
194void OTables::addComment(const Reference<XPropertySet>& descriptor, OUStringBuffer& _rOut)
195{
196 OUString sDesc;
197 descriptor->getPropertyValue(
199 >>= sDesc;
200 if (!sDesc.isEmpty())
201 {
202 _rOut.append(" COMMENT '");
203 _rOut.append(sDesc);
204 _rOut.append("'");
205 }
206}
207
208/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OptionalString sSchema
OptionalString sCatalog
OptionalString sComposedName
AnyEventRef aEvent
::dbtools::OPropertyMap & getPropMap()
Definition: TConnection.cxx:68
css::uno::Reference< css::sdbc::XDatabaseMetaData > m_xMetaData
Definition: HTables.hxx:28
void createTable(const css::uno::Reference< css::beans::XPropertySet > &descriptor)
virtual void disposing() override
Definition: HTables.cxx:86
virtual css::uno::Reference< css::beans::XPropertySet > createDescriptor() override
Definition: HTables.cxx:92
virtual void impl_refresh() override
Definition: HTables.cxx:81
virtual void dropObject(sal_Int32 _nPos, const OUString &_sElementName) override
Definition: HTables.cxx:105
virtual sdbcx::ObjectType appendObject(const OUString &_rForName, const css::uno::Reference< css::beans::XPropertySet > &descriptor) override
appends an object described by a descriptor, under a given name
void appendNew(const OUString &_rsNewTable)
Definition: HTables.cxx:158
virtual OUString getNameForObject(const sdbcx::ObjectType &_xObject) override
returns the name for the object.
Definition: HTables.cxx:169
virtual sdbcx::ObjectType createObject(const OUString &_rName) override
Definition: HTables.cxx:43
void dropByNameImpl(const OUString &elementName)
Definition: YViews.cxx:98
void insertElement(const OUString &_sElementName, const ObjectType &_xElement)
insert a new element into the collection
ObjectType getObject(sal_Int32 _nIndex)
return the object, if not existent it creates it.
::cppu::OWeakObject & m_rParent
Definition: VCollection.hxx:97
virtual sal_Bool SAL_CALL hasByName(const OUString &aName) override
::comphelper::OInterfaceContainerHelper3< css::container::XContainerListener > m_aContainerListeners
Definition: VCollection.hxx:93
const OUString & getNameByIndex(sal_Int32 _nIndex) const
Definition: propertyids.cxx:95
sal_Int32 nIndex
sal_uInt16 nPos
css::uno::Reference< css::beans::XPropertySet > ObjectType
Definition: VCollection.hxx:59
OUString createSqlCreateTableStatement(const Reference< XPropertySet > &descriptor, const Reference< XConnection > &_xConnection)
Definition: dbtools2.cxx:371
OUString composeTableName(const Reference< XDatabaseMetaData > &_rxMetaData, const OUString &_rCatalog, const OUString &_rSchema, const OUString &_rName, bool _bQuote, EComposeRule _eComposeRule)
Definition: dbtools.cxx:1286
Reference< XConnection > getConnection(const Reference< XRowSet > &_rxRowSet)
Definition: dbtools.cxx:348
void qualifiedNameComponents(const Reference< XDatabaseMetaData > &_rxConnMetaData, const OUString &_rQualifiedName, OUString &_rCatalog, OUString &_rSchema, OUString &_rName, EComposeRule _eComposeRule)
Definition: dbtools.cxx:862
#define PROPERTY_ID_TYPE
Definition: propertyids.hxx:51
#define PROPERTY_ID_DESCRIPTION
Definition: propertyids.hxx:58