From a11b728dae808d45e47565f375ba75104512b47e Mon Sep 17 00:00:00 2001 From: Tamas Bunth Date: Thu, 22 Mar 2018 11:39:16 +0100 Subject: dbahsql: refactor move index parser to parseschema MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I57820edc9ba8e9b8b11db78cf795fd5b1203db9b Reviewed-on: https://gerrit.libreoffice.org/51733 Tested-by: Jenkins Reviewed-by: Tamás Bunth --- dbaccess/source/filter/hsqldb/hsqlimport.cxx | 68 ++++----------------------- dbaccess/source/filter/hsqldb/parseschema.cxx | 61 ++++++++++++++++++++++-- dbaccess/source/filter/hsqldb/parseschema.hxx | 7 ++- 3 files changed, 73 insertions(+), 63 deletions(-) (limited to 'dbaccess/source') diff --git a/dbaccess/source/filter/hsqldb/hsqlimport.cxx b/dbaccess/source/filter/hsqldb/hsqlimport.cxx index 95f7de3a878a..0775fa580063 100644 --- a/dbaccess/source/filter/hsqldb/hsqlimport.cxx +++ b/dbaccess/source/filter/hsqldb/hsqlimport.cxx @@ -28,7 +28,7 @@ #include #include -#include +#include #include "hsqlimport.hxx" #include "parseschema.hxx" @@ -36,7 +36,6 @@ namespace { -using namespace ::comphelper; using namespace css::io; using namespace css::uno; using namespace css::sdbc; @@ -45,45 +44,6 @@ using ColumnTypeVector = std::vector; using RowVector = std::vector; using IndexVector = std::vector; -class IndexStmtParser -{ -private: - OUString m_sql; - -public: - IndexStmtParser(const OUString& sSql) - : m_sql(sSql) - { - } - - bool isIndexStatement() const - { - return m_sql.startsWith("SET TABLE") && m_sql.indexOf("INDEX") >= 0; - } - - IndexVector getIndexes() const - { - assert(isIndexStatement()); - - OUString sIndexPart = m_sql.copy(m_sql.indexOf("INDEX") + 5); - sal_Int32 nQuotePos = sIndexPart.indexOf("'") + 1; - OUString sIndexNums = sIndexPart.copy(nQuotePos, sIndexPart.lastIndexOf("'") - nQuotePos); - - std::vector sIndexes = string::split(sIndexNums, u' '); - IndexVector indexes; - for (const auto& sIndex : sIndexes) - indexes.push_back(sIndex.toInt32()); - - return indexes; - } - - OUString getTableName() const - { - // SET TABLE - return string::split(m_sql, u' ')[2]; - } -}; - void lcl_setParams(const RowVector& row, Reference& xParam, const ColumnTypeVector& rColTypes) { @@ -316,26 +276,18 @@ void HsqlImporter::importHsqlDatabase() SchemaParser parser(m_xStorage); SqlStatementVector statements = parser.parseSchema(); + // schema for (auto& sSql : statements) { - // SET TABLE ... INDEX ... - // These statements tell us the position of the data in the binary data - // file - IndexStmtParser aIndexParser(sSql); - if (aIndexParser.isIndexStatement()) - { - IndexVector aIndexes = aIndexParser.getIndexes(); - OUString sTableName = aIndexParser.getTableName(); - std::vector aColTypes = parser.getTableColumnTypes(sTableName); + Reference statement = m_rConnection->createStatement(); + statement->executeQuery(sSql); + } - parseTableRows(aIndexes, aColTypes, sTableName); - } - else - { - // other, "normal" statements - Reference statement = m_rConnection->createStatement(); - statement->executeQuery(sSql); - } + // data + for (const auto& tableIndex : parser.getTableIndexes()) + { + std::vector aColTypes = parser.getTableColumnTypes(tableIndex.first); + parseTableRows(tableIndex.second, aColTypes, tableIndex.first); } } } diff --git a/dbaccess/source/filter/hsqldb/parseschema.cxx b/dbaccess/source/filter/hsqldb/parseschema.cxx index 5ee2b4d1e2c6..c6331889adad 100644 --- a/dbaccess/source/filter/hsqldb/parseschema.cxx +++ b/dbaccess/source/filter/hsqldb/parseschema.cxx @@ -24,6 +24,53 @@ #include #include #include +#include + +namespace +{ +using namespace ::comphelper; + +using IndexVector = std::vector; + +class IndexStmtParser +{ +private: + OUString m_sql; + +public: + IndexStmtParser(const OUString& sSql) + : m_sql(sSql) + { + } + + bool isIndexStatement() const + { + return m_sql.startsWith("SET TABLE") && m_sql.indexOf("INDEX") >= 0; + } + + IndexVector getIndexes() const + { + assert(isIndexStatement()); + + OUString sIndexPart = m_sql.copy(m_sql.indexOf("INDEX") + 5); + sal_Int32 nQuotePos = sIndexPart.indexOf("'") + 1; + OUString sIndexNums = sIndexPart.copy(nQuotePos, sIndexPart.lastIndexOf("'") - nQuotePos); + + std::vector sIndexes = string::split(sIndexNums, u' '); + IndexVector indexes; + for (const auto& sIndex : sIndexes) + indexes.push_back(sIndex.toInt32()); + + return indexes; + } + + OUString getTableName() const + { + // SET TABLE + return string::split(m_sql, u' ')[2]; + } +}; +} // anonymous namespace namespace dbahsql { @@ -62,8 +109,10 @@ SqlStatementVector SchemaParser::parseSchema() // every line contains exactly one DDL statement OUString sSql = xTextInput->readLine(); - if (sSql.startsWith("SET TABLE") && sSql.indexOf("INDEX") > 0) - { // nothing + IndexStmtParser indexParser{ sSql }; + if (indexParser.isIndexStatement()) + { + m_ColumnTypes[indexParser.getTableName()] = indexParser.getIndexes(); } else if (sSql.startsWith("SET") || sSql.startsWith("CREATE USER") || sSql.startsWith("CREATE SCHEMA") || sSql.startsWith("GRANT")) @@ -82,9 +131,8 @@ SqlStatementVector SchemaParser::parseSchema() colTypes.push_back(colDef.getDataType()); m_ColumnTypes[aCreateParser.getTableName()] = colTypes; + parsedStatements.push_back(sSql); } - - parsedStatements.push_back(sSql); } return parsedStatements; @@ -95,6 +143,11 @@ ColumnTypeVector SchemaParser::getTableColumnTypes(const OUString& sTableName) c return m_ColumnTypes.at(sTableName); } +const std::map>& SchemaParser::getTableIndexes() const +{ + return m_Indexes; +} + } // namespace dbahsql /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/dbaccess/source/filter/hsqldb/parseschema.hxx b/dbaccess/source/filter/hsqldb/parseschema.hxx index af97ae578241..c0c31a3c6884 100644 --- a/dbaccess/source/filter/hsqldb/parseschema.hxx +++ b/dbaccess/source/filter/hsqldb/parseschema.hxx @@ -17,7 +17,7 @@ namespace dbahsql { -typedef std::vector SqlStatementVector; +using SqlStatementVector = std::vector; class SchemaParser { @@ -27,12 +27,17 @@ private: // column type for each table. It is filled after parsing schema. std::map> m_ColumnTypes; + // root element's position of data for each table + std::map> m_Indexes; + public: explicit SchemaParser(css::uno::Reference& rStorage); SqlStatementVector parseSchema(); std::vector getTableColumnTypes(const OUString& sTableName) const; + + const std::map>& getTableIndexes() const; }; } -- cgit