1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 dbtools
;
42 sdbcx::ObjectType
OTables::createObject(const OUString
& _rName
)
44 OUString sCatalog
, sSchema
, sTable
;
45 ::dbtools::qualifiedNameComponents(m_xMetaData
, _rName
, sCatalog
, sSchema
, sTable
,
46 ::dbtools::EComposeRule::InDataManipulation
);
48 Sequence
<OUString
> sTableTypes
{
49 u
"VIEW"_ustr
, u
"TABLE"_ustr
, u
"%"_ustr
50 }; // this last one just to be sure to include anything else...
53 if (!sCatalog
.isEmpty())
54 aCatalog
<<= sCatalog
;
55 Reference
<XResultSet
> xResult
= m_xMetaData
->getTables(aCatalog
, sSchema
, sTable
, sTableTypes
);
57 sdbcx::ObjectType xRet
;
60 Reference
<XRow
> xRow(xResult
, UNO_QUERY
);
61 if (xResult
->next()) // there can be only one table with this name
63 sal_Int32
const nPrivileges
= Privilege::DROP
| Privilege::REFERENCE
| Privilege::ALTER
64 | Privilege::CREATE
| Privilege::READ
| Privilege::DELETE
65 | Privilege::UPDATE
| Privilege::INSERT
68 xRet
= new OMySQLTable(this, static_cast<OMySQLCatalog
&>(m_rParent
).getConnection(),
69 sTable
, xRow
->getString(4), xRow
->getString(5), sSchema
,
70 sCatalog
, nPrivileges
);
72 ::comphelper::disposeComponent(xResult
);
78 void OTables::impl_refresh() { static_cast<OMySQLCatalog
&>(m_rParent
).refreshTables(); }
80 void OTables::disposing()
83 OCollection::disposing();
86 Reference
<XPropertySet
> OTables::createDescriptor()
88 return new OMySQLTable(this, static_cast<OMySQLCatalog
&>(m_rParent
).getConnection());
92 sdbcx::ObjectType
OTables::appendObject(const OUString
& _rForName
,
93 const Reference
<XPropertySet
>& descriptor
)
95 createTable(descriptor
);
96 return createObject(_rForName
);
100 void OTables::dropObject(sal_Int32 _nPos
, const OUString
& _sElementName
)
102 Reference
<XInterface
> xObject(getObject(_nPos
));
103 bool bIsNew
= connectivity::sdbcx::ODescriptor::isNew(xObject
);
107 Reference
<XConnection
> xConnection
= static_cast<OMySQLCatalog
&>(m_rParent
).getConnection();
109 OUString sCatalog
, sSchema
, sTable
;
110 ::dbtools::qualifiedNameComponents(m_xMetaData
, _sElementName
, sCatalog
, sSchema
, sTable
,
111 ::dbtools::EComposeRule::InDataManipulation
);
113 OUString
aSql(u
"DROP "_ustr
);
115 Reference
<XPropertySet
> xProp(xObject
, UNO_QUERY
);
116 bool bIsView
= xProp
.is()
117 && ::comphelper::getString(xProp
->getPropertyValue(
118 OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE
)))
120 if (bIsView
) // here we have a view
125 OUString
sComposedName(::dbtools::composeTableName(
126 m_xMetaData
, sCatalog
, sSchema
, sTable
, true, ::dbtools::EComposeRule::InDataManipulation
));
127 aSql
+= sComposedName
;
128 Reference
<XStatement
> xStmt
= xConnection
->createStatement();
131 xStmt
->execute(aSql
);
132 ::comphelper::disposeComponent(xStmt
);
134 // if no exception was thrown we must delete it from the views
138 = static_cast<OViews
*>(static_cast<OMySQLCatalog
&>(m_rParent
).getPrivateViews());
139 if (pViews
&& pViews
->hasByName(_sElementName
))
140 pViews
->dropByNameImpl(_sElementName
);
144 OUString
OTables::adjustSQL(const OUString
& _sSql
)
146 OUString sSQL
= _sSql
;
147 static const char s_sUNSIGNED
[] = "UNSIGNED";
148 sal_Int32 nIndex
= sSQL
.indexOf(s_sUNSIGNED
);
151 sal_Int32 nParen
= sSQL
.indexOf(')', nIndex
);
152 sal_Int32 nPos
= nIndex
+ strlen(s_sUNSIGNED
);
153 OUString
sNewUnsigned(sSQL
.copy(nPos
, nParen
- nPos
+ 1));
154 sSQL
= sSQL
.replaceAt(nIndex
, strlen(s_sUNSIGNED
) + sNewUnsigned
.getLength(),
155 rtl::Concat2View(sNewUnsigned
+ s_sUNSIGNED
));
156 nIndex
= sSQL
.indexOf(s_sUNSIGNED
, nIndex
+ strlen(s_sUNSIGNED
) + sNewUnsigned
.getLength());
161 void OTables::createTable(const Reference
<XPropertySet
>& descriptor
)
163 const Reference
<XConnection
> xConnection
164 = static_cast<OMySQLCatalog
&>(m_rParent
).getConnection();
166 = adjustSQL(::dbtools::createSqlCreateTableStatement(descriptor
, xConnection
));
167 Reference
<XStatement
> xStmt
= xConnection
->createStatement();
170 xStmt
->execute(aSql
);
171 ::comphelper::disposeComponent(xStmt
);
175 void OTables::appendNew(const OUString
& _rsNewTable
)
177 insertElement(_rsNewTable
, nullptr);
179 // notify our container listeners
180 ContainerEvent
aEvent(static_cast<XContainer
*>(this), Any(_rsNewTable
), Any(), Any());
181 OInterfaceIteratorHelper3
aListenerLoop(m_aContainerListeners
);
182 while (aListenerLoop
.hasMoreElements())
183 aListenerLoop
.next()->elementInserted(aEvent
);
186 OUString
OTables::getNameForObject(const sdbcx::ObjectType
& _xObject
)
188 OSL_ENSURE(_xObject
.is(), "OTables::getNameForObject: Object is NULL!");
189 return ::dbtools::composeTableName(m_xMetaData
, _xObject
,
190 ::dbtools::EComposeRule::InDataManipulation
, false);
193 void OTables::addComment(const Reference
<XPropertySet
>& descriptor
, OUStringBuffer
& _rOut
)
196 descriptor
->getPropertyValue(
197 OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DESCRIPTION
))
199 if (!sDesc
.isEmpty())
201 _rOut
.append(" COMMENT '");
207 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */