[Libreoffice-commits] core.git: dbaccess/source

Tamas Bunth tamas.bunth at collabora.co.uk
Wed May 2 12:10:00 UTC 2018


 dbaccess/source/filter/hsqldb/fbcreateparser.cxx |    8 ------
 dbaccess/source/filter/hsqldb/hsqlimport.cxx     |   11 +++++++-
 dbaccess/source/filter/hsqldb/parseschema.cxx    |   29 ++++++++++++++++-------
 dbaccess/source/filter/hsqldb/parseschema.hxx    |   15 ++++++++++-
 4 files changed, 44 insertions(+), 19 deletions(-)

New commits:
commit b18953565b68e46289ad85927d79f5978a51078b
Author: Tamas Bunth <tamas.bunth at collabora.co.uk>
Date:   Tue Apr 24 19:10:24 2018 +0200

    tdf#116980 Add constraints after data migration
    
    So there won't be any violation problems while migrating data.
    
    If there is a constraint created in a "CREATE TABLE..." statement, then
    cut it off and move it to a separate "ALTER TABLE ... ADD CONSTRAINT"
    statement.
    
    Change-Id: I7245ba8b23a6239cd3b724815a6385b9e6c17a91
    Reviewed-on: https://gerrit.libreoffice.org/53508
    Tested-by: Jenkins <ci at libreoffice.org>
    Reviewed-by: Tamás Bunth <btomi96 at gmail.com>

diff --git a/dbaccess/source/filter/hsqldb/fbcreateparser.cxx b/dbaccess/source/filter/hsqldb/fbcreateparser.cxx
index 3a16aab196a3..2208ce12d272 100644
--- a/dbaccess/source/filter/hsqldb/fbcreateparser.cxx
+++ b/dbaccess/source/filter/hsqldb/fbcreateparser.cxx
@@ -164,14 +164,6 @@ OUString FbCreateStmtParser::compose() const
             sSql.append(",");
     }
 
-    // foreign keys
-    const std::vector<OUString>& sForeignParts = getForeignParts();
-    for (const auto& sPart : sForeignParts)
-    {
-        sSql.append(",");
-        sSql.append(sPart);
-    }
-
     sSql.append(")"); // end of column declaration
     return sSql.makeStringAndClear();
 }
diff --git a/dbaccess/source/filter/hsqldb/hsqlimport.cxx b/dbaccess/source/filter/hsqldb/hsqlimport.cxx
index c5f77cef12ef..409ad54447d1 100644
--- a/dbaccess/source/filter/hsqldb/hsqlimport.cxx
+++ b/dbaccess/source/filter/hsqldb/hsqlimport.cxx
@@ -291,7 +291,9 @@ void HsqlImporter::importHsqlDatabase()
     assert(m_xStorage);
 
     SchemaParser parser(m_xStorage);
-    SqlStatementVector statements = parser.parseSchema();
+    parser.parseSchema();
+
+    auto statements = parser.getCreateStatements();
 
     if (statements.size() < 1)
     {
@@ -312,6 +314,13 @@ void HsqlImporter::importHsqlDatabase()
         std::vector<ColumnDefinition> aColTypes = parser.getTableColumnTypes(tableIndex.first);
         parseTableRows(tableIndex.second, aColTypes, tableIndex.first);
     }
+
+    // alter stmts
+    for (const auto& sSql : parser.getAlterStatements())
+    {
+        Reference<XStatement> statement = m_rConnection->createStatement();
+        statement->executeQuery(sSql);
+    }
 }
 }
 
diff --git a/dbaccess/source/filter/hsqldb/parseschema.cxx b/dbaccess/source/filter/hsqldb/parseschema.cxx
index 489f78a7d4b3..25f26ed7e626 100644
--- a/dbaccess/source/filter/hsqldb/parseschema.cxx
+++ b/dbaccess/source/filter/hsqldb/parseschema.cxx
@@ -76,6 +76,16 @@ public:
         return string::split(m_sql, u' ')[2];
     }
 };
+
+OUString lcl_createAlterForeign(const OUString& sForeignPart, const OUString& sTableName)
+{
+    OUStringBuffer sBuff("ALTER TABLE ");
+    sBuff.append(sTableName);
+    sBuff.append(" ADD ");
+    sBuff.append(sForeignPart);
+    return sBuff.makeStringAndClear();
+}
+
 } // anonymous namespace
 
 namespace dbahsql
@@ -91,7 +101,7 @@ SchemaParser::SchemaParser(Reference<XStorage>& rStorage)
 {
 }
 
-SqlStatementVector SchemaParser::parseSchema()
+void SchemaParser::parseSchema()
 {
     assert(m_rStorage);
 
@@ -99,7 +109,7 @@ SqlStatementVector SchemaParser::parseSchema()
     if (!m_rStorage->hasByName(SCHEMA_FILENAME))
     {
         SAL_WARN("dbaccess", "script file does not exist in storage during hsqldb import");
-        return SqlStatementVector{};
+        return;
     }
 
     Reference<XStream> xStream(m_rStorage->openStreamElement(SCHEMA_FILENAME, ElementModes::READ));
@@ -109,7 +119,6 @@ SqlStatementVector SchemaParser::parseSchema()
     xTextInput->setEncoding("UTF-8");
     xTextInput->setInputStream(xStream->getInputStream());
 
-    SqlStatementVector parsedStatements;
     while (!xTextInput->isEOF())
     {
         // every line contains exactly one DDL statement
@@ -128,11 +137,17 @@ SqlStatementVector SchemaParser::parseSchema()
             FbCreateStmtParser aCreateParser;
             aCreateParser.parse(sSql);
 
+            for (const auto& foreignParts : aCreateParser.getForeignParts())
+            {
+                m_sAlterStatements.push_back(
+                    lcl_createAlterForeign(foreignParts, aCreateParser.getTableName()));
+            }
+
             sSql = aCreateParser.compose();
 
             // save column definitions
             m_ColumnTypes[aCreateParser.getTableName()] = aCreateParser.getColumnDef();
-            parsedStatements.push_back(sSql);
+            m_sCreateStatements.push_back(sSql);
         }
         else if (sSql.startsWith("ALTER"))
         {
@@ -141,13 +156,11 @@ SqlStatementVector SchemaParser::parseSchema()
             OUString parsedStmt = aAlterParser.compose();
 
             if (!parsedStmt.isEmpty())
-                parsedStatements.push_back(parsedStmt);
+                m_sAlterStatements.push_back(parsedStmt);
         }
         else if (sSql.startsWith("CREATE VIEW"))
-            parsedStatements.push_back(sSql);
+            m_sCreateStatements.push_back(sSql);
     }
-
-    return parsedStatements;
 }
 
 ColumnTypeVector SchemaParser::getTableColumnTypes(const OUString& sTableName) const
diff --git a/dbaccess/source/filter/hsqldb/parseschema.hxx b/dbaccess/source/filter/hsqldb/parseschema.hxx
index 94e72c815717..a46e7ae4fdb6 100644
--- a/dbaccess/source/filter/hsqldb/parseschema.hxx
+++ b/dbaccess/source/filter/hsqldb/parseschema.hxx
@@ -32,15 +32,26 @@ private:
     // root element's position of data for each table
     std::map<OUString, std::vector<sal_Int32>> m_Indexes;
 
+    SqlStatementVector m_sCreateStatements;
+    SqlStatementVector m_sAlterStatements;
+
 public:
     explicit SchemaParser(css::uno::Reference<css::embed::XStorage>& rStorage);
 
     /**
      * Parses table definitions contained by a file called "script" in storage.
-     *
+     */
+    void parseSchema();
+
+    /**
      * @return A vector of schema definition SQL strings in Firebird dialect.
      */
-    SqlStatementVector parseSchema();
+    const SqlStatementVector& getCreateStatements() { return m_sCreateStatements; }
+
+    /**
+     * @return A vector of alter SQL strings in Firebird dialect.
+     */
+    const SqlStatementVector& getAlterStatements() { return m_sAlterStatements; }
 
     /**
      * Returns the column types of a table. It should not be called before


More information about the Libreoffice-commits mailing list