LibreOffice Module connectivity (master) 1
mysqlc_tables.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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
10#include "mysqlc_table.hxx"
11#include "mysqlc_tables.hxx"
12#include "mysqlc_catalog.hxx"
13
14#include <TConnection.hxx>
15
17
18#include <com/sun/star/sdbc/XRow.hpp>
19#include <com/sun/star/sdbc/ColumnValue.hpp>
20#include <comphelper/types.hxx>
21
22//----- OCollection -----------------------------------------------------------
24{
25 static_cast<Catalog&>(m_rParent).refreshTables();
26}
27
28static void lcl_unescape(OUString& rName)
29{
30 // Remove ending ` if there's one
31 sal_Int32 nLastIndexBacktick = rName.lastIndexOf("`");
32 if ((nLastIndexBacktick > 0) && (nLastIndexBacktick == (rName.getLength() - 1)))
33 {
34 rName = rName.copy(0, nLastIndexBacktick);
35 }
36
37 // Remove beginning `
38 nLastIndexBacktick = rName.indexOf("`");
39 if (nLastIndexBacktick == 0)
40 {
41 rName = rName.copy(1, rName.getLength() - 1);
42 }
43
44 // Replace double ` by simple `
45 rName = rName.replaceAll("``", "`");
46}
47
49{
50 OUString sCatalog, sSchema, sTable;
51 ::dbtools::qualifiedNameComponents(m_xMetaData, rName, sCatalog, sSchema, sTable,
52 ::dbtools::EComposeRule::InDataManipulation);
53
54 css::uno::Any aCatalog;
55 if (!sCatalog.isEmpty())
56 {
58 aCatalog <<= sCatalog;
59 }
60
62 lcl_unescape(sTable);
63
64 // Only retrieving a single table, so table type is irrelevant (param 4)
65 css::uno::Reference<css::sdbc::XResultSet> xTables
66 = m_xMetaData->getTables(aCatalog, sSchema, sTable, css::uno::Sequence<OUString>());
67
68 if (!xTables.is())
69 throw css::uno::RuntimeException("Could not acquire table.");
70
71 css::uno::Reference<css::sdbc::XRow> xRow(xTables, css::uno::UNO_QUERY_THROW);
72
73 if (!xTables->next())
74 throw css::uno::RuntimeException();
75
77 new Table(this, m_rMutex, m_xMetaData->getConnection(),
78 xRow->getString(1), // Catalog
79 xRow->getString(2), // Schema
80 xRow->getString(3), // Name
81 xRow->getString(4), // Type
82 xRow->getString(5))); // Description / Remarks / Comments
83
84 if (xTables->next())
85 throw css::uno::RuntimeException("Found more tables than expected.");
86
87 return xRet;
88}
89
90css::uno::Reference<css::beans::XPropertySet> connectivity::mysqlc::Tables::createDescriptor()
91{
92 // There is some internal magic so that the same class can be used as either
93 // a descriptor or as a normal table. See VTable.cxx for the details. In our
94 // case we just need to ensure we use the correct constructor.
95 return new Table(this, m_rMutex, m_xMetaData->getConnection());
96}
97
98//----- XAppend ---------------------------------------------------------------
100 const css::uno::Reference<css::beans::XPropertySet>& descriptor)
101{
102 const css::uno::Reference<css::sdbc::XConnection> xConnection = m_xMetaData->getConnection();
103 OUString aSql = ::dbtools::createSqlCreateTableStatement(descriptor, xConnection);
104
105 css::uno::Reference<css::sdbc::XStatement> xStmt = xConnection->createStatement();
106 if (xStmt.is())
107 {
108 xStmt->execute(aSql);
109 ::comphelper::disposeComponent(xStmt);
110 }
111}
112
113// XAppend
115 const OUString& _rForName, const css::uno::Reference<css::beans::XPropertySet>& descriptor)
116{
117 createTable(descriptor);
118 return createObject(_rForName);
119}
120
121void connectivity::mysqlc::Tables::appendNew(const OUString& _rsNewTable)
122{
123 insertElement(_rsNewTable, nullptr);
124
125 // notify our container listeners
126 css::container::ContainerEvent aEvent(static_cast<XContainer*>(this),
127 css::uno::Any(_rsNewTable), css::uno::Any(),
128 css::uno::Any());
129 comphelper::OInterfaceIteratorHelper3 aListenerLoop(m_aContainerListeners);
130 while (aListenerLoop.hasMoreElements())
131 aListenerLoop.next()->elementInserted(aEvent);
132}
133
134OUString
136{
137 OSL_ENSURE(_xObject.is(), "OTables::getNameForObject: Object is NULL!");
138 return ::dbtools::composeTableName(m_xMetaData, _xObject,
139 ::dbtools::EComposeRule::InDataManipulation, false)
140 .replaceAll(u"`", u"̀ `");
141}
142
143//----- XDrop -----------------------------------------------------------------
144void connectivity::mysqlc::Tables::dropObject(sal_Int32 nPosition, const OUString& sName)
145{
146 css::uno::Reference<css::beans::XPropertySet> xTable(getObject(nPosition));
147
149 return;
150
151 OUString sType;
152 xTable->getPropertyValue("Type") >>= sType;
153
154 m_xMetaData->getConnection()->createStatement()->execute("DROP " + sType + " " + sName);
155}
156
157/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
OptionalString sSchema
OptionalString sCatalog
OptionalString sType
AnyEventRef aEvent
css::uno::Reference< ListenerT > const & next()
void createTable(const css::uno::Reference< css::beans::XPropertySet > &descriptor)
virtual css::uno::Reference< css::beans::XPropertySet > createDescriptor() override
virtual ::connectivity::sdbcx::ObjectType createObject(const OUString &rName) override
virtual void dropObject(sal_Int32 nPosition, const OUString &rName) override
virtual void impl_refresh() override
void appendNew(const OUString &_rsNewTable)
virtual OUString getNameForObject(const sdbcx::ObjectType &_xObject) override
returns the name for the object.
virtual ::connectivity::sdbcx::ObjectType appendObject(const OUString &rName, const css::uno::Reference< css::beans::XPropertySet > &rDescriptor) override
appends an object described by a descriptor, under a given name
::cppu::OWeakObject & m_rParent
Definition: VCollection.hxx:97
::osl::Mutex & m_rMutex
float u
OUString sName
static void lcl_unescape(OUString &rName)
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
void qualifiedNameComponents(const Reference< XDatabaseMetaData > &_rxConnMetaData, const OUString &_rQualifiedName, OUString &_rCatalog, OUString &_rSchema, OUString &_rName, EComposeRule _eComposeRule)
Definition: dbtools.cxx:862