summaryrefslogtreecommitdiffstats
path: root/connectivity/source/drivers/mysqlc/mysqlc_tables.cxx
blob: 81498978d3a57bad5272dd05c452f6f55fe4987e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include "mysqlc_table.hxx"
#include "mysqlc_tables.hxx"
#include "mysqlc_catalog.hxx"

#include <TConnection.hxx>

#include <connectivity/dbtools.hxx>

#include <com/sun/star/sdbc/XRow.hpp>
#include <com/sun/star/sdbc/ColumnValue.hpp>
#include <comphelper/types.hxx>

//----- OCollection -----------------------------------------------------------
void connectivity::mysqlc::Tables::impl_refresh()
{
    static_cast<Catalog&>(m_rParent).refreshTables();
}

static void lcl_unescape(OUString& rName)
{
    // Remove ending ` if there's one
    sal_Int32 nLastIndexBacktick = rName.lastIndexOf("`");
    if ((nLastIndexBacktick > 0) && (nLastIndexBacktick == (rName.getLength() - 1)))
    {
        rName = rName.copy(0, nLastIndexBacktick);
    }

    // Remove beginning `
    nLastIndexBacktick = rName.indexOf("`");
    if (nLastIndexBacktick == 0)
    {
        rName = rName.copy(1, rName.getLength() - 1);
    }

    // Replace double ` by simple `
    rName = rName.replaceAll("``", "`");
}

connectivity::sdbcx::ObjectType connectivity::mysqlc::Tables::createObject(const OUString& rName)
{
    OUString sCatalog, sSchema, sTable;
    ::dbtools::qualifiedNameComponents(m_xMetaData, rName, sCatalog, sSchema, sTable,
                                       ::dbtools::EComposeRule::InDataManipulation);

    css::uno::Any aCatalog;
    if (!sCatalog.isEmpty())
    {
        lcl_unescape(sCatalog);
        aCatalog <<= sCatalog;
    }

    lcl_unescape(sSchema);
    lcl_unescape(sTable);

    // Only retrieving a single table, so table type is irrelevant (param 4)
    css::uno::Reference<css::sdbc::XResultSet> xTables
        = m_xMetaData->getTables(aCatalog, sSchema, sTable, css::uno::Sequence<OUString>());

    if (!xTables.is())
        throw css::uno::RuntimeException("Could not acquire table.");

    css::uno::Reference<css::sdbc::XRow> xRow(xTables, css::uno::UNO_QUERY_THROW);

    if (!xTables->next())
        throw css::uno::RuntimeException();

    connectivity::sdbcx::ObjectType xRet(
        new Table(this, m_rMutex, m_xMetaData->getConnection(),
                  xRow->getString(1), // Catalog
                  xRow->getString(2), // Schema
                  xRow->getString(3), // Name
                  xRow->getString(4), // Type
                  xRow->getString(5))); // Description / Remarks / Comments

    if (xTables->next())
        throw css::uno::RuntimeException("Found more tables than expected.");

    return xRet;
}

css::uno::Reference<css::beans::XPropertySet> connectivity::mysqlc::Tables::createDescriptor()
{
    // There is some internal magic so that the same class can be used as either
    // a descriptor or as a normal table. See VTable.cxx for the details. In our
    // case we just need to ensure we use the correct constructor.
    return new Table(this, m_rMutex, m_xMetaData->getConnection());
}

//----- XAppend ---------------------------------------------------------------
void connectivity::mysqlc::Tables::createTable(
    const css::uno::Reference<css::beans::XPropertySet>& descriptor)
{
    const css::uno::Reference<css::sdbc::XConnection> xConnection = m_xMetaData->getConnection();
    OUString aSql = ::dbtools::createSqlCreateTableStatement(descriptor, xConnection);

    css::uno::Reference<css::sdbc::XStatement> xStmt = xConnection->createStatement();
    if (xStmt.is())
    {
        xStmt->execute(aSql);
        ::comphelper::disposeComponent(xStmt);
    }
}

// XAppend
connectivity::sdbcx::ObjectType connectivity::mysqlc::Tables::appendObject(
    const OUString& _rForName, const css::uno::Reference<css::beans::XPropertySet>& descriptor)
{
    createTable(descriptor);
    return createObject(_rForName);
}

void connectivity::mysqlc::Tables::appendNew(const OUString& _rsNewTable)
{
    insertElement(_rsNewTable, nullptr);

    // notify our container listeners
    css::container::ContainerEvent aEvent(static_cast<XContainer*>(this),
                                          css::uno::Any(_rsNewTable), css::uno::Any(),
                                          css::uno::Any());
    comphelper::OInterfaceIteratorHelper3 aListenerLoop(m_aContainerListeners);
    while (aListenerLoop.hasMoreElements())
        aListenerLoop.next()->elementInserted(aEvent);
}

OUString
connectivity::mysqlc::Tables::getNameForObject(const connectivity::sdbcx::ObjectType& _xObject)
{
    OSL_ENSURE(_xObject.is(), "OTables::getNameForObject: Object is NULL!");
    return ::dbtools::composeTableName(m_xMetaData, _xObject,
                                       ::dbtools::EComposeRule::InDataManipulation, false)
        .replaceAll(u"`", u"̀ `");
}

//----- XDrop -----------------------------------------------------------------
void connectivity::mysqlc::Tables::dropObject(sal_Int32 nPosition, const OUString& sName)
{
    css::uno::Reference<css::beans::XPropertySet> xTable(getObject(nPosition));

    if (connectivity::sdbcx::ODescriptor::isNew(xTable))
        return;

    OUString sType;
    xTable->getPropertyValue("Type") >>= sType;

    m_xMetaData->getConnection()->createStatement()->execute("DROP " + sType + " " + sName);
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */