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:
@@ -28,7 +28,7 @@
|
|||||||
#include <com/sun/star/sdbc/XParameters.hpp>
|
#include <com/sun/star/sdbc/XParameters.hpp>
|
||||||
#include <com/sun/star/sdbc/DataType.hpp>
|
#include <com/sun/star/sdbc/DataType.hpp>
|
||||||
|
|
||||||
#include <comphelper/string.hxx>
|
#include <rtl/ustrbuf.hxx>
|
||||||
|
|
||||||
#include "hsqlimport.hxx"
|
#include "hsqlimport.hxx"
|
||||||
#include "parseschema.hxx"
|
#include "parseschema.hxx"
|
||||||
@@ -36,7 +36,6 @@
|
|||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
using namespace ::comphelper;
|
|
||||||
using namespace css::io;
|
using namespace css::io;
|
||||||
using namespace css::uno;
|
using namespace css::uno;
|
||||||
using namespace css::sdbc;
|
using namespace css::sdbc;
|
||||||
@@ -45,45 +44,6 @@ using ColumnTypeVector = std::vector<sal_Int32>;
|
|||||||
using RowVector = std::vector<Any>;
|
using RowVector = std::vector<Any>;
|
||||||
using IndexVector = std::vector<sal_Int32>;
|
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,
|
void lcl_setParams(const RowVector& row, Reference<XParameters>& xParam,
|
||||||
const ColumnTypeVector& rColTypes)
|
const ColumnTypeVector& rColTypes)
|
||||||
{
|
{
|
||||||
@@ -316,26 +276,18 @@ void HsqlImporter::importHsqlDatabase()
|
|||||||
SchemaParser parser(m_xStorage);
|
SchemaParser parser(m_xStorage);
|
||||||
SqlStatementVector statements = parser.parseSchema();
|
SqlStatementVector statements = parser.parseSchema();
|
||||||
|
|
||||||
|
// schema
|
||||||
for (auto& sSql : statements)
|
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);
|
|
||||||
|
|
||||||
parseTableRows(aIndexes, aColTypes, sTableName);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// other, "normal" statements
|
|
||||||
Reference<XStatement> statement = m_rConnection->createStatement();
|
Reference<XStatement> statement = m_rConnection->createStatement();
|
||||||
statement->executeQuery(sSql);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -24,6 +24,53 @@
|
|||||||
#include <com/sun/star/embed/XStorage.hpp>
|
#include <com/sun/star/embed/XStorage.hpp>
|
||||||
#include <com/sun/star/embed/ElementModes.hpp>
|
#include <com/sun/star/embed/ElementModes.hpp>
|
||||||
#include <comphelper/processfactory.hxx>
|
#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
|
namespace dbahsql
|
||||||
{
|
{
|
||||||
@@ -62,8 +109,10 @@ SqlStatementVector SchemaParser::parseSchema()
|
|||||||
// every line contains exactly one DDL statement
|
// every line contains exactly one DDL statement
|
||||||
OUString sSql = xTextInput->readLine();
|
OUString sSql = xTextInput->readLine();
|
||||||
|
|
||||||
if (sSql.startsWith("SET TABLE") && sSql.indexOf("INDEX") > 0)
|
IndexStmtParser indexParser{ sSql };
|
||||||
{ // nothing
|
if (indexParser.isIndexStatement())
|
||||||
|
{
|
||||||
|
m_ColumnTypes[indexParser.getTableName()] = indexParser.getIndexes();
|
||||||
}
|
}
|
||||||
else if (sSql.startsWith("SET") || sSql.startsWith("CREATE USER")
|
else if (sSql.startsWith("SET") || sSql.startsWith("CREATE USER")
|
||||||
|| sSql.startsWith("CREATE SCHEMA") || sSql.startsWith("GRANT"))
|
|| sSql.startsWith("CREATE SCHEMA") || sSql.startsWith("GRANT"))
|
||||||
@@ -82,10 +131,9 @@ SqlStatementVector SchemaParser::parseSchema()
|
|||||||
colTypes.push_back(colDef.getDataType());
|
colTypes.push_back(colDef.getDataType());
|
||||||
|
|
||||||
m_ColumnTypes[aCreateParser.getTableName()] = colTypes;
|
m_ColumnTypes[aCreateParser.getTableName()] = colTypes;
|
||||||
}
|
|
||||||
|
|
||||||
parsedStatements.push_back(sSql);
|
parsedStatements.push_back(sSql);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return parsedStatements;
|
return parsedStatements;
|
||||||
}
|
}
|
||||||
@@ -95,6 +143,11 @@ ColumnTypeVector SchemaParser::getTableColumnTypes(const OUString& sTableName) c
|
|||||||
return m_ColumnTypes.at(sTableName);
|
return m_ColumnTypes.at(sTableName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const std::map<OUString, std::vector<sal_Int32>>& SchemaParser::getTableIndexes() const
|
||||||
|
{
|
||||||
|
return m_Indexes;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace dbahsql
|
} // namespace dbahsql
|
||||||
|
|
||||||
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|
||||||
|
@@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
namespace dbahsql
|
namespace dbahsql
|
||||||
{
|
{
|
||||||
typedef std::vector<OUString> SqlStatementVector;
|
using SqlStatementVector = std::vector<OUString>;
|
||||||
|
|
||||||
class SchemaParser
|
class SchemaParser
|
||||||
{
|
{
|
||||||
@@ -27,12 +27,17 @@ private:
|
|||||||
// column type for each table. It is filled after parsing schema.
|
// column type for each table. It is filled after parsing schema.
|
||||||
std::map<OUString, std::vector<sal_Int32>> m_ColumnTypes;
|
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:
|
public:
|
||||||
explicit SchemaParser(css::uno::Reference<css::embed::XStorage>& rStorage);
|
explicit SchemaParser(css::uno::Reference<css::embed::XStorage>& rStorage);
|
||||||
|
|
||||||
SqlStatementVector parseSchema();
|
SqlStatementVector parseSchema();
|
||||||
|
|
||||||
std::vector<sal_Int32> getTableColumnTypes(const OUString& sTableName) const;
|
std::vector<sal_Int32> getTableColumnTypes(const OUString& sTableName) const;
|
||||||
|
|
||||||
|
const std::map<OUString, std::vector<sal_Int32>>& getTableIndexes() const;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user