dbaccess/CppunitTest_dbaccess_hsqlschema_import.mk |  101 +++++++++++
 dbaccess/Module_dbaccess.mk                        |    1 
 dbaccess/qa/extras/hsql_schema_import.cxx          |  179 +++++++++++++++++++++
 dbaccess/source/filter/hsqldb/createparser.hxx     |    2 
 dbaccess/source/filter/hsqldb/fbcreateparser.hxx   |    2 
 5 files changed, 283 insertions(+), 2 deletions(-)

New commits:
commit 9adc7bdf6d304c540339f389aa02c925733ef248
Author: Tamas Bunth <tamas.bu...@collabora.co.uk>
Date:   Mon Mar 19 11:09:09 2018 +0100

    dbahsql: add unit tests for schema import
    
    Change-Id: Ib666562f2d33824188ea3d643e6056f375a4339b
    Reviewed-on: https://gerrit.libreoffice.org/51537
    Tested-by: Jenkins <c...@libreoffice.org>
    Reviewed-by: Tamás Bunth <btom...@gmail.com>

diff --git a/dbaccess/CppunitTest_dbaccess_hsqlschema_import.mk 
b/dbaccess/CppunitTest_dbaccess_hsqlschema_import.mk
new file mode 100644
index 000000000000..8c8d06698b38
--- /dev/null
+++ b/dbaccess/CppunitTest_dbaccess_hsqlschema_import.mk
@@ -0,0 +1,101 @@
+# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
+#
+# This file is part of the LibreOffice project.
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+#
+
+$(eval $(call gb_CppunitTest_CppunitTest,dbaccess_hsql_schema_import))
+
+$(eval $(call 
gb_CppunitTest_add_exception_objects,dbaccess_hsql_schema_import, \
+       dbaccess/qa/extras/hsql_schema_import \
+))
+
+$(eval $(call gb_CppunitTest_use_libraries,dbaccess_hsql_schema_import, \
+       basegfx \
+       comphelper \
+       cppu \
+       cppuhelper \
+       dba \
+       dbahsql \
+       dbu \
+       sdbt \
+       drawinglayer \
+       editeng \
+       for \
+       forui \
+       i18nlangtag \
+       msfilter \
+       oox \
+       sal \
+       salhelper \
+       sax \
+       sb \
+       sfx \
+       sot \
+       svl \
+       svt \
+       svx \
+       svxcore \
+       test \
+       subsequenttest \
+       tl \
+       tk \
+       ucbhelper \
+       unotest \
+       utl \
+       vbahelper \
+       vcl \
+       xo \
+))
+
+$(eval $(call gb_CppunitTest_set_include,dbaccess_hsql_schema_import,\
+       -I$(SRCDIR)/dbaccess/source/filter/hsqldb \
+       $$(INCLUDE) \
+))
+
+$(eval $(call gb_CppunitTest_use_api,dbaccess_hsql_schema_import,\
+       offapi \
+       oovbaapi \
+       udkapi \
+))
+
+$(eval $(call gb_CppunitTest_use_components,dbaccess_hsql_schema_import,\
+       basic/util/sb \
+       comphelper/util/comphelp \
+       configmgr/source/configmgr \
+       dbaccess/util/dba \
+       dbaccess/util/dbu \
+       dbaccess/util/sdbt \
+       dbaccess/source/filter/xml/dbaxml \
+       filter/source/config/cache/filterconfig1 \
+       forms/util/frm \
+       framework/util/fwk \
+       i18npool/util/i18npool \
+       linguistic/source/lng \
+       oox/util/oox \
+       package/source/xstor/xstor \
+       package/util/package2 \
+       sax/source/expatwrap/expwrap \
+       scripting/source/basprov/basprov \
+       scripting/util/scriptframe \
+       sfx2/util/sfx \
+       sot/util/sot \
+       svl/source/fsstor/fsstorage \
+       svl/util/svl \
+       toolkit/util/tk \
+       ucb/source/core/ucb1 \
+       ucb/source/ucp/file/ucpfile1 \
+       ucb/source/ucp/tdoc/ucptdoc1 \
+       unotools/util/utl \
+       unoxml/source/rdf/unordf \
+       unoxml/source/service/unoxml \
+       uui/util/uui \
+       xmloff/util/xo \
+))
+
+$(eval $(call gb_CppunitTest_use_configuration,dbaccess_hsql_schema_import))
+
+# vim: set noet sw=4 ts=4:
diff --git a/dbaccess/Module_dbaccess.mk b/dbaccess/Module_dbaccess.mk
index 9bc2253f7465..a8ecb1060c43 100644
--- a/dbaccess/Module_dbaccess.mk
+++ b/dbaccess/Module_dbaccess.mk
@@ -44,6 +44,7 @@ $(eval $(call gb_Module_add_check_targets,dbaccess,\
        CppunitTest_dbaccess_empty_stdlib_save \
        CppunitTest_dbaccess_nolib_save \
        CppunitTest_dbaccess_macros_test \
+       CppunitTest_dbaccess_hsqlschema_import \
 ))
 
 ifeq ($(ENABLE_JAVA),TRUE)
diff --git a/dbaccess/qa/extras/hsql_schema_import.cxx 
b/dbaccess/qa/extras/hsql_schema_import.cxx
new file mode 100644
index 000000000000..a04e3697ecfb
--- /dev/null
+++ b/dbaccess/qa/extras/hsql_schema_import.cxx
@@ -0,0 +1,179 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <fbcreateparser.hxx>
+#include <columndef.hxx>
+#include <cppunit/plugin/TestPlugIn.h>
+#include <com/sun/star/sdbc/DataType.hpp>
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+using namespace dbahsql;
+
+namespace
+{
+constexpr std::size_t operator"" _z(unsigned long long n) { return n; }
+
+const ColumnDefinition* lcl_findByType(const std::vector<ColumnDefinition>& 
columns,
+                                       sal_Int32 nType)
+{
+    for (const auto& column : columns)
+    {
+        if (column.getDataType() == nType)
+            return &column;
+    }
+    return nullptr;
+}
+}
+
+class HsqlSchemaImportTest : public CppUnit::TestFixture
+{
+public:
+    void testIntegerPrimaryKeyNotNull();
+    void testVarcharWithParam();
+    void testVarcharWithoutParam();
+    void testNumericWithTwoParam();
+    void testIntegerAutoincremental();
+    void testTimestampWithParam();
+    // TODO testForeign, testDecomposer
+
+    CPPUNIT_TEST_SUITE(HsqlSchemaImportTest);
+
+    CPPUNIT_TEST(testIntegerPrimaryKeyNotNull);
+    CPPUNIT_TEST(testVarcharWithParam);
+    CPPUNIT_TEST(testVarcharWithoutParam);
+    CPPUNIT_TEST(testNumericWithTwoParam);
+    CPPUNIT_TEST(testIntegerAutoincremental);
+    CPPUNIT_TEST(testTimestampWithParam);
+
+    CPPUNIT_TEST_SUITE_END();
+};
+
+void HsqlSchemaImportTest::testIntegerPrimaryKeyNotNull()
+{
+    OUString sql{ "CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL 
PRIMARY KEY)" };
+
+    FbCreateStmtParser aCreateParser;
+    aCreateParser.parse(sql);
+
+    CPPUNIT_ASSERT_EQUAL(OUString{ "\"myTable\"" }, 
aCreateParser.getTableName());
+    const auto& columns = aCreateParser.getColumnDef();
+    CPPUNIT_ASSERT_EQUAL(1_z, columns.size());
+    const auto& column = columns.at(0);
+    CPPUNIT_ASSERT_EQUAL(OUString{ "\"id\"" }, column.getName());
+    CPPUNIT_ASSERT_EQUAL(css::sdbc::DataType::INTEGER, column.getDataType());
+    CPPUNIT_ASSERT(column.isPrimaryKey());
+    CPPUNIT_ASSERT(!column.isNullable());
+    CPPUNIT_ASSERT(!column.isAutoIncremental());
+}
+
+void HsqlSchemaImportTest::testVarcharWithParam()
+{
+    OUString sql{ "CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL 
PRIMARY KEY, \"myText\" "
+                  "VARCHAR(50))" };
+
+    FbCreateStmtParser aCreateParser;
+    aCreateParser.parse(sql);
+
+    const auto& columns = aCreateParser.getColumnDef();
+    CPPUNIT_ASSERT_EQUAL(2_z, columns.size());
+    const ColumnDefinition* colVarchar = lcl_findByType(columns, 
css::sdbc::DataType::VARCHAR);
+    CPPUNIT_ASSERT(colVarchar != nullptr);
+    const auto& params = colVarchar->getParams();
+    CPPUNIT_ASSERT_EQUAL(1_z, params.size());
+    constexpr sal_Int32 nParamExpected = 50;
+    CPPUNIT_ASSERT_EQUAL(nParamExpected, params.at(0)); // VARCHAR(50)
+}
+
+/**
+ * Special case:
+ * HSQLDB might define a column VARCHAR without parameter. With Firebird
+ * dialect, this is forbidden, so a default parameter has to be appended:
+ **/
+void HsqlSchemaImportTest::testVarcharWithoutParam()
+{
+    OUString sql{ "CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL 
PRIMARY KEY, \"myText\" "
+                  "VARCHAR)" };
+
+    FbCreateStmtParser aCreateParser;
+    aCreateParser.parse(sql);
+
+    const auto& columns = aCreateParser.getColumnDef();
+    CPPUNIT_ASSERT_EQUAL(2_z, columns.size());
+    const ColumnDefinition* colVarchar = lcl_findByType(columns, 
css::sdbc::DataType::VARCHAR);
+    CPPUNIT_ASSERT(colVarchar != nullptr);
+    const auto& params = colVarchar->getParams();
+    CPPUNIT_ASSERT_EQUAL(1_z, params.size()); // parameter generated
+}
+
+void HsqlSchemaImportTest::testNumericWithTwoParam()
+{
+    OUString sql{ "CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL 
PRIMARY KEY, \"Betrag\" "
+                  "NUMERIC(8,2))" };
+
+    FbCreateStmtParser aCreateParser;
+    aCreateParser.parse(sql);
+
+    const auto& columns = aCreateParser.getColumnDef();
+    CPPUNIT_ASSERT_EQUAL(2_z, columns.size());
+
+    const ColumnDefinition* colNumeric = lcl_findByType(columns, 
css::sdbc::DataType::NUMERIC);
+    CPPUNIT_ASSERT(colNumeric != nullptr);
+    const auto& params = colNumeric->getParams();
+    CPPUNIT_ASSERT_EQUAL(2_z, params.size());
+
+    constexpr sal_Int32 nPrecision = 8;
+    constexpr sal_Int32 nScale = 2;
+    CPPUNIT_ASSERT_EQUAL(nPrecision, params.at(0));
+    CPPUNIT_ASSERT_EQUAL(nScale, params.at(1));
+}
+
+void HsqlSchemaImportTest::testIntegerAutoincremental()
+{
+    OUString sql{ "CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL 
PRIMARY KEY GENERATED "
+                  "BY DEFAULT AS IDENTITY(START WITH 0), \"myText\" 
VARCHAR(50))" };
+
+    FbCreateStmtParser aCreateParser;
+    aCreateParser.parse(sql);
+
+    const auto& columns = aCreateParser.getColumnDef();
+    const auto column = columns.at(0);
+    CPPUNIT_ASSERT_EQUAL(css::sdbc::DataType::INTEGER, column.getDataType());
+    CPPUNIT_ASSERT(column.isAutoIncremental());
+    CPPUNIT_ASSERT(column.isPrimaryKey());
+    CPPUNIT_ASSERT(!column.isNullable());
+}
+
+/**
+ * Special case:
+ * Hsqldb might use one parameter for defining column with type TIMESTAMP.
+ * With Firebird this is illegal.
+ */
+void HsqlSchemaImportTest::testTimestampWithParam()
+{
+    OUString sql{ "CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL 
PRIMARY KEY, \"myText\" "
+                  "TIMESTAMP(0))" };
+
+    FbCreateStmtParser aCreateParser;
+    aCreateParser.parse(sql);
+
+    const auto& columns = aCreateParser.getColumnDef();
+    const ColumnDefinition* colTimeStamp = lcl_findByType(columns, 
css::sdbc::DataType::TIMESTAMP);
+
+    CPPUNIT_ASSERT(colTimeStamp != nullptr);
+
+    // instead of asserting parameter size, look at the deparsed string,
+    // because it's Firebird specific
+    OUString fbSql = aCreateParser.compose();
+    CPPUNIT_ASSERT(fbSql.indexOf("0") < 0); //does not contain
+}
+
+CPPUNIT_TEST_SUITE_REGISTRATION(HsqlSchemaImportTest);
+
+CPPUNIT_PLUGIN_IMPLEMENT();
diff --git a/dbaccess/source/filter/hsqldb/createparser.hxx 
b/dbaccess/source/filter/hsqldb/createparser.hxx
index 03532a1d197f..5aa6bc0e9b16 100644
--- a/dbaccess/source/filter/hsqldb/createparser.hxx
+++ b/dbaccess/source/filter/hsqldb/createparser.hxx
@@ -15,7 +15,7 @@
 
 namespace dbahsql
 {
-class CreateStmtParser
+class SAL_DLLPUBLIC_EXPORT CreateStmtParser
 {
 private:
     std::vector<ColumnDefinition> m_aColumns;
diff --git a/dbaccess/source/filter/hsqldb/fbcreateparser.hxx 
b/dbaccess/source/filter/hsqldb/fbcreateparser.hxx
index fec8c6f9c34b..98b90158051a 100644
--- a/dbaccess/source/filter/hsqldb/fbcreateparser.hxx
+++ b/dbaccess/source/filter/hsqldb/fbcreateparser.hxx
@@ -14,7 +14,7 @@
 
 namespace dbahsql
 {
-class FbCreateStmtParser : public CreateStmtParser
+class SAL_DLLPUBLIC_EXPORT FbCreateStmtParser : public CreateStmtParser
 {
 public:
     FbCreateStmtParser() {}
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to