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>
This commit is contained in:
Tamas Bunth
2018-03-22 11:39:16 +01:00
committed by Tamás Bunth
parent fdd3941acf
commit a11b728dae
3 changed files with 73 additions and 63 deletions

View File

@@ -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);
}
}
}

View File

@@ -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: */

View File

@@ -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;
};
}