diff options
author | Julien Nabet <serval2412@yahoo.fr> | 2022-01-18 22:32:08 +0100 |
---|---|---|
committer | Julien Nabet <serval2412@yahoo.fr> | 2022-01-20 08:40:54 +0100 |
commit | 578d26b3160db5df03a6cf9dcd447fa38bb36ed0 (patch) | |
tree | 35ca10ef7783f108df0d2247f9cd15f33c4de4a4 /connectivity | |
parent | f7a5be583f0b3b99f7e9def6be8be02ae645bd75 (diff) |
Mysql/MariaDB: deal a bit better schema/table with existing functions
In "Tables::appendObject":
- Simplify code by relying on :dbtools::createSqlCreateTableStatement
- Call "createObject" with "sComposedName" instead of awkward "sSchema + "." + rName"
and since "createObject" decomposes its parameter with ::dbtools::qualifiedNameComponents
we can also get rid of the ugly "CONCAT(TABLE_SCHEMA, '.', TABLE_NAME)" and just use "TABLE_NAME"
In ODatabaseMetaData::getTables, we must remove "`" since it seems incompatible with LIKE
(or missed how to make it compatible)
Also remove useless Keys::dropObject.
It seems we can rely on general mechanism since adding/removing primary key works
and so remove m_pTable which also become useless
bonus: avoid "using namespace dbtools" in mysqlc_views.cxx for 2 uses only
Change-Id: Ic24ec84c61dea7d419fc3a05ded6545bf20455bd
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/128579
Tested-by: Jenkins
Reviewed-by: Julien Nabet <serval2412@yahoo.fr>
Diffstat (limited to 'connectivity')
5 files changed, 40 insertions, 72 deletions
diff --git a/connectivity/source/drivers/mysqlc/mysqlc_databasemetadata.cxx b/connectivity/source/drivers/mysqlc/mysqlc_databasemetadata.cxx index adaaa57a7e6f..57856804c793 100644 --- a/connectivity/source/drivers/mysqlc/mysqlc_databasemetadata.cxx +++ b/connectivity/source/drivers/mysqlc/mysqlc_databasemetadata.cxx @@ -806,8 +806,7 @@ Reference<XResultSet> SAL_CALL ODatabaseMetaData::getTables(const Any& /*catalog "IF(STRCMP(TABLE_TYPE,'BASE TABLE'), TABLE_TYPE, 'TABLE') AS TABLE_TYPE, TABLE_COMMENT AS " "REMARKS " "FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA NOT IN ('information_schema', 'mysql', " - "'performance_schema') AND TABLE_SCHEMA LIKE '?' AND CONCAT(TABLE_SCHEMA, '.', TABLE_NAME) " - "LIKE '?' " + "'performance_schema') AND TABLE_SCHEMA LIKE '?' AND TABLE_NAME LIKE '?' " }; if (types.getLength() == 1) diff --git a/connectivity/source/drivers/mysqlc/mysqlc_keys.cxx b/connectivity/source/drivers/mysqlc/mysqlc_keys.cxx index 770cead57be1..d5d97f892fc7 100644 --- a/connectivity/source/drivers/mysqlc/mysqlc_keys.cxx +++ b/connectivity/source/drivers/mysqlc/mysqlc_keys.cxx @@ -15,7 +15,6 @@ using namespace ::connectivity; using namespace ::connectivity::mysqlc; -using namespace ::dbtools; using namespace ::osl; using namespace ::com::sun::star; @@ -25,29 +24,7 @@ using namespace ::com::sun::star::uno; Keys::Keys(Table* pTable, Mutex& rMutex, const ::std::vector<OUString>& rNames) : OKeysHelper(pTable, rMutex, rNames) - , m_pTable(pTable) { } -//----- XDrop ---------------------------------------------------------------- -void Keys::dropObject(sal_Int32 nPosition, const OUString& sName) -{ - // TODO: implement (should we just copy from Firebird LO code below?) - if (m_pTable->isNew()) - return; - - uno::Reference<XPropertySet> xKey(getObject(nPosition), UNO_QUERY); - - if (xKey.is()) - { - const OUString sQuote - = m_pTable->getConnection()->getMetaData()->getIdentifierQuoteString(); - - OUString sSql("ALTER TABLE " + quoteName(sQuote, m_pTable->getTableName()) - + " DROP CONSTRAINT " + quoteName(sQuote, sName)); - - m_pTable->getConnection()->createStatement()->execute(sSql); - } -} - /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/connectivity/source/drivers/mysqlc/mysqlc_keys.hxx b/connectivity/source/drivers/mysqlc/mysqlc_keys.hxx index b967b9387792..9d0a9ef1cc43 100644 --- a/connectivity/source/drivers/mysqlc/mysqlc_keys.hxx +++ b/connectivity/source/drivers/mysqlc/mysqlc_keys.hxx @@ -17,14 +17,8 @@ class Table; class Keys : public ::connectivity::OKeysHelper { -private: - Table* m_pTable; - public: Keys(Table* pTable, ::osl::Mutex& rMutex, const ::std::vector<OUString>& rNames); - - // OKeysHelper / XDrop - void dropObject(sal_Int32 nPosition, const OUString& sName) override; }; } diff --git a/connectivity/source/drivers/mysqlc/mysqlc_tables.cxx b/connectivity/source/drivers/mysqlc/mysqlc_tables.cxx index 8bf019d9bb36..14cf061d8d52 100644 --- a/connectivity/source/drivers/mysqlc/mysqlc_tables.cxx +++ b/connectivity/source/drivers/mysqlc/mysqlc_tables.cxx @@ -37,11 +37,45 @@ using namespace ::com::sun::star::uno; //----- OCollection ----------------------------------------------------------- void 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); + } + + // Remove beginning ` and replace double ` by simple ` + rName = rName.replaceAll("``", "`"); +} + ObjectType Tables::createObject(const OUString& rName) { + OUString sCatalog, sSchema, sTable; + ::dbtools::qualifiedNameComponents(m_xMetaData, rName, sCatalog, sSchema, sTable, + ::dbtools::EComposeRule::InDataManipulation); + + Any aCatalog; + if (!sCatalog.isEmpty()) + { + aCatalog <<= sCatalog; + lcl_unescape(sCatalog); + } + + lcl_unescape(sSchema); + lcl_unescape(sTable); + // Only retrieving a single table, so table type is irrelevant (param 4) uno::Reference<XResultSet> xTables - = m_xMetaData->getTables(Any(), u"%", rName, uno::Sequence<OUString>()); + = m_xMetaData->getTables(aCatalog, sSchema, sTable, uno::Sequence<OUString>()); if (!xTables.is()) throw RuntimeException("Could not acquire table."); @@ -130,12 +164,11 @@ uno::Reference<XPropertySet> Tables::createDescriptor() } //----- XAppend --------------------------------------------------------------- -ObjectType Tables::appendObject(const OUString& rName, +ObjectType Tables::appendObject(const OUString& /* rName */, const uno::Reference<XPropertySet>& rDescriptor) { - /* OUString sSql(::dbtools::createSqlCreateTableStatement(rDescriptor, - m_xMetaData->getConnection())); */ - OUStringBuffer aSqlBuffer("CREATE TABLE "); + OUString sSql( + ::dbtools::createSqlCreateTableStatement(rDescriptor, m_xMetaData->getConnection())); OUString sCatalog, sSchema, sComposedName, sTable; const Reference<XConnection>& xConnection = m_xMetaData->getConnection(); @@ -150,43 +183,9 @@ ObjectType Tables::appendObject(const OUString& rName, if (sComposedName.isEmpty()) ::dbtools::throwFunctionSequenceException(xConnection); - aSqlBuffer.append(sComposedName); - aSqlBuffer.append(" ("); - - // columns - Reference<XColumnsSupplier> xColumnSup(rDescriptor, UNO_QUERY); - Reference<XIndexAccess> xColumns(xColumnSup->getColumns(), UNO_QUERY); - // check if there are columns - if (!xColumns.is() || !xColumns->getCount()) - ::dbtools::throwFunctionSequenceException(xConnection); - - Reference<XPropertySet> xColProp; - - sal_Int32 nCount = xColumns->getCount(); - for (sal_Int32 i = 0; i < nCount; ++i) - { - if ((xColumns->getByIndex(i) >>= xColProp) && xColProp.is()) - { - aSqlBuffer.append(createStandardColumnPart(xColProp, xConnection)); - aSqlBuffer.append(","); - } - } - OUString sSql = aSqlBuffer.makeStringAndClear(); - - const OUString sKeyStmt = ::dbtools::createStandardKeyStatement(rDescriptor, xConnection); - if (!sKeyStmt.isEmpty()) - sSql += sKeyStmt; - else - { - if (sSql.endsWith(",")) - sSql = sSql.replaceAt(sSql.getLength() - 1, 1, u")"); - else - sSql += ")"; - } - m_xMetaData->getConnection()->createStatement()->execute(sSql); - return createObject(sSchema + "." + rName); + return createObject(sComposedName); } //----- XDrop ----------------------------------------------------------------- diff --git a/connectivity/source/drivers/mysqlc/mysqlc_views.cxx b/connectivity/source/drivers/mysqlc/mysqlc_views.cxx index bdc2f718fd6f..5db17db83c85 100644 --- a/connectivity/source/drivers/mysqlc/mysqlc_views.cxx +++ b/connectivity/source/drivers/mysqlc/mysqlc_views.cxx @@ -36,7 +36,6 @@ using namespace css::sdbcx; using namespace css::sdbc; using namespace css::container; using namespace css::lang; -using namespace dbtools; typedef connectivity::sdbcx::OCollection OCollection_TYPE; Views::Views(const Reference<XConnection>& _rxConnection, ::cppu::OWeakObject& _rParent, |