LibreOffice Module connectivity (master) 1
mysqlc_table.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_columns.hxx"
11#include "mysqlc_indexes.hxx"
12#include "mysqlc_keys.hxx"
13#include "mysqlc_table.hxx"
14
15#include <TConnection.hxx>
16
17#include <comphelper/types.hxx>
19
20#include <com/sun/star/sdbcx/Privilege.hpp>
21#include <com/sun/star/beans/PropertyAttribute.hpp>
22
24 Tables* pTables, osl::Mutex& rMutex,
25 const css::uno::Reference<OMetaConnection::XConnection>& rConnection)
26 : OTableHelper(pTables, rConnection, true)
27 , m_rMutex(rMutex)
28 , m_nPrivileges(0)
29{
30 construct();
31}
32
34 Tables* pTables, osl::Mutex& rMutex,
35 const css::uno::Reference<OMetaConnection::XConnection>& rConnection, const OUString& rCatalog,
36 const OUString& rSchema, const OUString& rName, const OUString& rType,
37 const OUString& rDescription)
38 : OTableHelper(pTables, rConnection, true, rName, rType, rDescription, rSchema, rCatalog)
39 , m_rMutex(rMutex)
40 , m_nPrivileges(0)
41{
42 construct();
43}
44
46{
48 if (isNew())
49 return;
50
51 // TODO: get privileges when in non-embedded mode.
52 m_nPrivileges = css::sdbcx::Privilege::DROP | css::sdbcx::Privilege::REFERENCE
53 | css::sdbcx::Privilege::ALTER | css::sdbcx::Privilege::CREATE
54 | css::sdbcx::Privilege::READ | css::sdbcx::Privilege::DELETE
55 | css::sdbcx::Privilege::UPDATE | css::sdbcx::Privilege::INSERT
56 | css::sdbcx::Privilege::SELECT;
58 PROPERTY_ID_PRIVILEGES, css::beans::PropertyAttribute::READONLY,
59 &m_nPrivileges, cppu::UnoType<decltype(m_nPrivileges)>::get());
60}
61//----- OTableHelper ---------------------------------------------------------
63connectivity::mysqlc::Table::createColumns(const ::std::vector<OUString>& rNames)
64{
65 return new Columns(*this, m_rMutex, rNames);
66}
67
69connectivity::mysqlc::Table::createKeys(const ::std::vector<OUString>& rNames)
70{
71 return new Keys(this, m_rMutex, rNames);
72}
73
75connectivity::mysqlc::Table::createIndexes(const ::std::vector<OUString>& rNames)
76{
77 return new Indexes(this, m_rMutex, rNames);
78}
79
80//----- XAlterTable -----------------------------------------------------------
82 const OUString& rColName, const css::uno::Reference<XPropertySet>& rDescriptor)
83{
84 osl::MutexGuard aGuard(m_rMutex);
85 checkDisposed(WeakComponentImplHelperBase::rBHelper.bDisposed);
86
87 css::uno::Reference<XPropertySet> xColumn(m_xColumns->getByName(rColName), css::uno::UNO_QUERY);
88
89 // sdbcx::Descriptor
90 const bool bNameChanged
91 = xColumn->getPropertyValue("Name") != rDescriptor->getPropertyValue("Name");
92 // sdbcx::ColumnDescriptor
93 const bool bTypeChanged
94 = xColumn->getPropertyValue("Type") != rDescriptor->getPropertyValue("Type");
95 const bool bTypeNameChanged = !comphelper::getString(xColumn->getPropertyValue("TypeName"))
96 .equalsIgnoreAsciiCase(comphelper::getString(
97 rDescriptor->getPropertyValue("TypeName")));
98 const bool bPrecisionChanged
99 = xColumn->getPropertyValue("Precision") != rDescriptor->getPropertyValue("Precision");
100 const bool bScaleChanged
101 = xColumn->getPropertyValue("Scale") != rDescriptor->getPropertyValue("Scale");
102
103 const bool bIsNullableChanged
104 = xColumn->getPropertyValue("IsNullable") != rDescriptor->getPropertyValue("IsNullable");
105
106 const bool bIsAutoIncrementChanged = xColumn->getPropertyValue("IsAutoIncrement")
107 != rDescriptor->getPropertyValue("IsAutoIncrement");
108
109 // there's also DefaultValue but not related to database directly, it seems completely internal to LO
110 // so no need to test it
111 // TODO: remainder -- these are all "optional" so have to detect presence and change.
112 if (bTypeChanged || bTypeNameChanged || bPrecisionChanged || bScaleChanged || bIsNullableChanged
113 || bIsAutoIncrementChanged)
114 {
115 // If bPrecisionChanged this will only succeed if we have increased the
116 // precision, otherwise an exception is thrown -- however the base
117 // gui then offers to delete and recreate the column.
118 OUStringBuffer sSql(300);
119 sSql.append(getAlterTableColumnPart() + " MODIFY COLUMN `" + rColName + "` "
121
122 if (comphelper::getBOOL(rDescriptor->getPropertyValue("IsAutoIncrement")))
123 sSql.append(" auto_increment");
124
125 // see ColumnValue: NO_NULLS = 0, NULLABLE = 1, NULLABLE_UNKNOWN
126 // so entry required = yes corresponds to NO_NULLS = 0 and only in this case
127 // NOT NULL
128 if (comphelper::getINT32(rDescriptor->getPropertyValue("IsNullable")) == 0)
129 sSql.append(" NOT NULL");
130
131 getConnection()->createStatement()->execute(sSql.makeStringAndClear());
132 }
133
134 if (bNameChanged)
135 {
136 OUString sNewColName;
137 rDescriptor->getPropertyValue("Name") >>= sNewColName;
138 OUString sSql(getAlterTableColumnPart() + " RENAME COLUMN `" + rColName + "` TO `"
139 + sNewColName + "`");
140
141 getConnection()->createStatement()->execute(sSql);
142 }
143
144 m_xColumns->refresh();
145}
146
148 sal_Int32 index, const css::uno::Reference<css::beans::XPropertySet>& descriptor)
149{
150 osl::MutexGuard aGuard(m_rMutex);
151 css::uno::Reference<XPropertySet> xColumn(m_xColumns->getByIndex(index),
152 css::uno::UNO_QUERY_THROW);
153 alterColumnByName(comphelper::getString(xColumn->getPropertyValue(
155 descriptor);
156}
157
159{
160 return "ALTER TABLE "
161 + ::dbtools::composeTableName(getMetaData(), m_CatalogName, m_SchemaName, m_Name, true,
162 ::dbtools::EComposeRule::InTableDefinitions);
163}
164
165OUString connectivity::mysqlc::Table::getRenameStart() const { return "RENAME TABLE "; }
166
167/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
OUString m_Name
void registerProperty(const OUString &_rName, sal_Int32 _nHandle, sal_Int32 _nAttributes, void *_pPointerToMember, const css::uno::Type &_rMemberType)
::dbtools::OPropertyMap & getPropMap()
Definition: TConnection.cxx:68
virtual ::connectivity::sdbcx::OCollection * createIndexes(const ::std::vector< OUString > &rNames) override
creates the index collection for the table
virtual void SAL_CALL alterColumnByIndex(sal_Int32 index, const css::uno::Reference< css::beans::XPropertySet > &descriptor) override
virtual ::connectivity::sdbcx::OCollection * createKeys(const ::std::vector< OUString > &rNames) override
creates the key collection for the table
OUString getAlterTableColumnPart() const
returns the ALTER TABLE XXX statement
Table(Tables *pTables, ::osl::Mutex &rMutex, const css::uno::Reference< css::sdbc::XConnection > &_xConnection)
virtual ::connectivity::sdbcx::OCollection * createColumns(const ::std::vector< OUString > &rNames) override
creates the column collection for the table
virtual OUString getRenameStart() const override
Returns always "RENAME TABLE " even for views.
virtual void SAL_CALL alterColumnByName(const OUString &rColName, const css::uno::Reference< css::beans::XPropertySet > &rDescriptor) override
See css::sdbcx::ColumnDescriptor for details of rDescriptor.
virtual void construct() override
Definition: VTable.cxx:86
const OUString & getNameByIndex(sal_Int32 _nIndex) const
Definition: propertyids.cxx:95
::osl::Mutex & m_rMutex
Reference< XColumn > xColumn
bool getBOOL(const Any &_rAny)
sal_Int32 getINT32(const Any &_rAny)
OUString getString(const Any &_rAny)
void checkDisposed(bool _bThrow)
OUString composeTableName(const Reference< XDatabaseMetaData > &_rxMetaData, const OUString &_rCatalog, const OUString &_rSchema, const OUString &_rName, bool _bQuote, EComposeRule _eComposeRule)
Definition: dbtools.cxx:1286
OUString createStandardTypePart(const Reference< XPropertySet > &xColProp, const Reference< XConnection > &_xConnection, std::u16string_view _sCreatePattern)
Definition: dbtools2.cxx:67
Reference< XConnection > getConnection(const Reference< XRowSet > &_rxRowSet)
Definition: dbtools.cxx:348
index
#define PROPERTY_ID_NAME
Definition: propertyids.hxx:50
#define PROPERTY_ID_PRIVILEGES
Definition: propertyids.hxx:94