29#include <com/sun/star/io/TextInputStream.hpp>
30#include <com/sun/star/embed/XStorage.hpp>
31#include <com/sun/star/embed/ElementModes.hpp>
42using IndexVector = std::vector<sal_Int32>;
50 IndexStmtParser(OUString sSql)
51 : m_sql(
std::move(sSql))
55 bool isIndexStatement()
const
57 return m_sql.startsWith(
"SET TABLE") && m_sql.indexOf(
"INDEX") >= 0;
60 IndexVector getIndexes()
const
62 assert(isIndexStatement());
64 std::u16string_view sIndexPart = m_sql.subView(m_sql.indexOf(
"INDEX") + 5);
65 size_t nQuotePos = sIndexPart.find(
'\'');
66 if (nQuotePos == std::u16string_view::npos)
70 std::u16string_view sIndexNums
71 = sIndexPart.substr(nQuotePos, sIndexPart.rfind(
'\'') - nQuotePos);
73 std::vector<OUString> sIndexes = string::split(sIndexNums, u
' ');
75 for (
const auto& sIndex : sIndexes)
76 indexes.push_back(sIndex.toInt32());
86 OUString getTableName()
const
89 OUString
sName = string::split(m_sql, u
' ')[2];
90 if (
sName.indexOf(
'"') >= 0)
93 sName =
"\"" + string::split(m_sql, u
'"')[1] +
"\"";
99OUString lcl_createAlterForeign(std::u16string_view sForeignPart, std::u16string_view sTableName)
101 return OUString::Concat(
"ALTER TABLE ") + sTableName +
" ADD " + sForeignPart;
108using namespace css::io;
109using namespace css::uno;
110using namespace css::embed;
121 static constexpr OUStringLiteral SCHEMA_FILENAME =
u"script";
124 SAL_WARN(
"dbaccess",
"script file does not exist in storage during hsqldb import");
128 Reference<XStream>
xStream(
m_rStorage->openStreamElement(SCHEMA_FILENAME, ElementModes::READ));
131 Reference<XTextInputStream2> xTextInput = TextInputStream::create(rContext);
132 xTextInput->setEncoding(
"UTF-8");
133 xTextInput->setInputStream(
xStream->getInputStream());
135 while (!xTextInput->isEOF())
141 IndexStmtParser indexParser{ sSql };
142 if (indexParser.isIndexStatement())
144 m_Indexes[indexParser.getTableName()] = indexParser.getIndexes();
146 else if (sSql.startsWith(
"SET") || sSql.startsWith(
"CREATE USER")
147 || sSql.startsWith(
"CREATE SCHEMA") || sSql.startsWith(
"GRANT"))
149 else if (sSql.startsWith(
"CREATE CACHED TABLE") || sSql.startsWith(
"CREATE TABLE"))
152 aCreateParser.
parse(sSql);
157 lcl_createAlterForeign(foreignParts, aCreateParser.
getTableName()));
160 sSql = aCreateParser.
compose();
167 else if (sSql.startsWith(
"ALTER"))
170 aAlterParser.
parse(sSql);
171 OUString parsedStmt = aAlterParser.
compose();
173 if (!parsedStmt.isEmpty())
176 else if (sSql.startsWith(
"CREATE VIEW"))
185 static constexpr OUStringLiteral NOT_EXIST
186 =
u"Internal error while getting column information of table";
187 SAL_WARN(
"dbaccess", NOT_EXIST <<
". Table name is: " << sTableName);
void parse(const OUString &sSql)
const std::vector< OUString > & getForeignParts() const
void parse(std::u16string_view sSql)
Parses a create statement.
OUString const & getTableName() const
const std::vector< ColumnDefinition > & getColumnDef() const
virtual OUString compose() const override
Compose the result of the parser to statements of Firebird dialect.
virtual OUString compose() const override
Compose the result of the parser to statements of Firebird dialect.
std::map< OUString, std::vector< sal_Int32 > > m_Indexes
SqlStatementVector m_sCreateStatements
void parseSchema()
Parses table definitions contained by a file called "script" in storage.
const std::map< OUString, std::vector< OUString > > & getPrimaryKeys() const
Returns a vector of column names for each table.
std::map< OUString, std::vector< ColumnDefinition > > m_ColumnTypes
std::vector< ColumnDefinition > getTableColumnTypes(const OUString &sTableName) const
Returns the column types of a table.
const std::map< OUString, std::vector< sal_Int32 > > & getTableIndexes() const
Returns a vector of indexes for each table.
SqlStatementVector m_sAlterStatements
std::map< OUString, std::vector< OUString > > m_PrimaryKeys
css::uno::Reference< css::embed::XStorage > & m_rStorage
SchemaParser(css::uno::Reference< css::embed::XStorage > &rStorage)
#define SAL_WARN(area, stream)
Reference< XComponentContext > getProcessComponentContext()
OUString convertToUTF8(std::string_view original)
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)