diff options
author | Tamas Bunth <tamas.bunth@collabora.co.uk> | 2018-03-22 11:39:16 +0100 |
---|---|---|
committer | Tamás Bunth <btomi96@gmail.com> | 2018-03-22 13:49:00 +0100 |
commit | a11b728dae808d45e47565f375ba75104512b47e (patch) | |
tree | 22e7cbe8b61afb03875a9be47d39233fb40845e0 /dbaccess/source | |
parent | fdd3941acfb3e9f4c1775d50210f9811cd179564 (diff) |
dbahsql: refactor move index parser to parseschema
Change-Id: I57820edc9ba8e9b8b11db78cf795fd5b1203db9b
Reviewed-on: https://gerrit.libreoffice.org/51733
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Tamás Bunth <btomi96@gmail.com>
Diffstat (limited to 'dbaccess/source')
-rw-r--r-- | dbaccess/source/filter/hsqldb/hsqlimport.cxx | 68 | ||||
-rw-r--r-- | dbaccess/source/filter/hsqldb/parseschema.cxx | 61 | ||||
-rw-r--r-- | dbaccess/source/filter/hsqldb/parseschema.hxx | 7 |
3 files changed, 73 insertions, 63 deletions
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 <com/sun/star/sdbc/XParameters.hpp> #include <com/sun/star/sdbc/DataType.hpp> -#include <comphelper/string.hxx> +#include <rtl/ustrbuf.hxx> #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<sal_Int32>; using RowVector = std::vector<Any>; using IndexVector = std::vector<sal_Int32>; -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<OUString> sIndexes = string::split(sIndexNums, u' '); - IndexVector indexes; - for (const auto& sIndex : sIndexes) - indexes.push_back(sIndex.toInt32()); - - return indexes; - } - - OUString getTableName() const - { - // SET TABLE <tableName> - return string::split(m_sql, u' ')[2]; - } -}; - void lcl_setParams(const RowVector& row, Reference<XParameters>& 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<sal_Int32> aColTypes = parser.getTableColumnTypes(sTableName); + Reference<XStatement> statement = m_rConnection->createStatement(); + statement->executeQuery(sSql); + } - parseTableRows(aIndexes, aColTypes, sTableName); - } - else - { - // other, "normal" statements - Reference<XStatement> statement = m_rConnection->createStatement(); - statement->executeQuery(sSql); - } + // data + for (const auto& tableIndex : parser.getTableIndexes()) + { + std::vector<sal_Int32> 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 <com/sun/star/embed/XStorage.hpp> #include <com/sun/star/embed/ElementModes.hpp> #include <comphelper/processfactory.hxx> +#include <comphelper/string.hxx> + +namespace +{ +using namespace ::comphelper; + +using IndexVector = std::vector<sal_Int32>; + +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<OUString> sIndexes = string::split(sIndexNums, u' '); + IndexVector indexes; + for (const auto& sIndex : sIndexes) + indexes.push_back(sIndex.toInt32()); + + return indexes; + } + + OUString getTableName() const + { + // SET TABLE <tableName> + 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<OUString, std::vector<sal_Int32>>& 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<OUString> SqlStatementVector; +using SqlStatementVector = std::vector<OUString>; class SchemaParser { @@ -27,12 +27,17 @@ private: // column type for each table. It is filled after parsing schema. std::map<OUString, std::vector<sal_Int32>> m_ColumnTypes; + // root element's position of data for each table + std::map<OUString, std::vector<sal_Int32>> m_Indexes; + public: explicit SchemaParser(css::uno::Reference<css::embed::XStorage>& rStorage); SqlStatementVector parseSchema(); std::vector<sal_Int32> getTableColumnTypes(const OUString& sTableName) const; + + const std::map<OUString, std::vector<sal_Int32>>& getTableIndexes() const; }; } |