diff options
author | Tamas Bunth <tamas.bunth@collabora.co.uk> | 2018-06-26 12:27:57 +0200 |
---|---|---|
committer | Tamás Bunth <btomi96@gmail.com> | 2018-07-06 10:33:48 +0200 |
commit | 8f3d6e67b08ed1bce9bc989e5e3df92c4d9d95d3 (patch) | |
tree | 090693bcf6074ace894d37a6b40c0dab01706c6f | |
parent | 2b0a7c346f8e3c5823a9e032a4501f20bc712914 (diff) |
tdf#117867 dbahsql: Migrate default values
Default Value "NOW" is a special case, because Firebird likes it with
single quotes. In contrast, HSQLDB uses the keyword without quotes.
Add unit test for parsing default value "NOW"
Change-Id: I34886f8ae53e98addb52bf5e85030a03721d6a45
Reviewed-on: https://gerrit.libreoffice.org/56454
Tested-by: Jenkins
Reviewed-by: Tamás Bunth <btomi96@gmail.com>
-rw-r--r-- | dbaccess/qa/extras/hsql_schema_import.cxx | 23 | ||||
-rw-r--r-- | dbaccess/source/filter/hsqldb/columndef.cxx | 4 | ||||
-rw-r--r-- | dbaccess/source/filter/hsqldb/columndef.hxx | 4 | ||||
-rw-r--r-- | dbaccess/source/filter/hsqldb/createparser.cxx | 18 | ||||
-rw-r--r-- | dbaccess/source/filter/hsqldb/fbcreateparser.cxx | 10 |
5 files changed, 56 insertions, 3 deletions
diff --git a/dbaccess/qa/extras/hsql_schema_import.cxx b/dbaccess/qa/extras/hsql_schema_import.cxx index a04e3697ecfb..892be33d15e7 100644 --- a/dbaccess/qa/extras/hsql_schema_import.cxx +++ b/dbaccess/qa/extras/hsql_schema_import.cxx @@ -41,6 +41,7 @@ public: void testNumericWithTwoParam(); void testIntegerAutoincremental(); void testTimestampWithParam(); + void testDefaultValueNow(); // TODO testForeign, testDecomposer CPPUNIT_TEST_SUITE(HsqlSchemaImportTest); @@ -51,6 +52,7 @@ public: CPPUNIT_TEST(testNumericWithTwoParam); CPPUNIT_TEST(testIntegerAutoincremental); CPPUNIT_TEST(testTimestampWithParam); + CPPUNIT_TEST(testDefaultValueNow); CPPUNIT_TEST_SUITE_END(); }; @@ -174,6 +176,27 @@ void HsqlSchemaImportTest::testTimestampWithParam() CPPUNIT_ASSERT(fbSql.indexOf("0") < 0); //does not contain } +/** + * Special case: + * HSQLDB uses keyword NOW without quotes. Firebird uses single quotes 'NOW' + */ +void HsqlSchemaImportTest::testDefaultValueNow() +{ + OUString sql{ "CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL PRIMARY KEY, \"myDate\" " + "TIMESTAMP DEFAULT NOW)" }; + + FbCreateStmtParser aCreateParser; + aCreateParser.parse(sql); + + const auto& columns = aCreateParser.getColumnDef(); + const ColumnDefinition* colTimeStamp = lcl_findByType(columns, css::sdbc::DataType::TIMESTAMP); + + CPPUNIT_ASSERT(colTimeStamp != nullptr); + CPPUNIT_ASSERT_EQUAL(OUString{ "NOW" }, colTimeStamp->getDefault()); // parsed NOW + OUString fbSql = aCreateParser.compose(); + CPPUNIT_ASSERT(fbSql.indexOf("\'NOW\'") > 0); // composed 'NOW' +} + CPPUNIT_TEST_SUITE_REGISTRATION(HsqlSchemaImportTest); CPPUNIT_PLUGIN_IMPLEMENT(); diff --git a/dbaccess/source/filter/hsqldb/columndef.cxx b/dbaccess/source/filter/hsqldb/columndef.cxx index 6e36ee1b8718..dd71e81f28b3 100644 --- a/dbaccess/source/filter/hsqldb/columndef.cxx +++ b/dbaccess/source/filter/hsqldb/columndef.cxx @@ -27,7 +27,8 @@ using namespace css::sdbc; ColumnDefinition::ColumnDefinition(const OUString& sName, sal_Int32 eType, const std::vector<sal_Int32> aParams, bool bPrimary, - sal_Int32 nAutoIncr, bool bNullable, bool bCaseInsensitive) + sal_Int32 nAutoIncr, bool bNullable, bool bCaseInsensitive, + const OUString& sDefault) : m_sName(sName) , m_eType(eType) , m_aParams(aParams) @@ -35,6 +36,7 @@ ColumnDefinition::ColumnDefinition(const OUString& sName, sal_Int32 eType, , m_nAutoIncrement(nAutoIncr) , m_bNullable(bNullable) , m_bCaseInsensitive(bCaseInsensitive) + , m_sDefaultValue(sDefault) { } } diff --git a/dbaccess/source/filter/hsqldb/columndef.hxx b/dbaccess/source/filter/hsqldb/columndef.hxx index b356d3906b37..fd23ed313abb 100644 --- a/dbaccess/source/filter/hsqldb/columndef.hxx +++ b/dbaccess/source/filter/hsqldb/columndef.hxx @@ -26,11 +26,12 @@ private: sal_Int32 m_nAutoIncrement; bool m_bNullable; bool m_bCaseInsensitive; + OUString m_sDefaultValue; public: ColumnDefinition(const OUString& sName, sal_Int32 eType, const std::vector<sal_Int32> aParams, bool bPrimary = false, sal_Int32 nAutoIncr = -1, bool bNullable = true, - bool bCaseInsensitive = false); + bool bCaseInsensitive = false, const OUString& sDefault = OUString{}); OUString const& getName() const { return m_sName; } sal_Int32 getDataType() const { return m_eType; } @@ -40,6 +41,7 @@ public: bool isCaseInsensitive() const { return m_bCaseInsensitive; } sal_Int32 getStartValue() const { return m_nAutoIncrement; } const std::vector<sal_Int32>& getParams() const { return m_aParams; } + OUString const& getDefault() const { return m_sDefaultValue; } }; } diff --git a/dbaccess/source/filter/hsqldb/createparser.cxx b/dbaccess/source/filter/hsqldb/createparser.cxx index 5b864f4231c6..11443e7b4d37 100644 --- a/dbaccess/source/filter/hsqldb/createparser.cxx +++ b/dbaccess/source/filter/hsqldb/createparser.cxx @@ -111,6 +111,21 @@ sal_Int32 lcl_getAutoIncrementDefault(const OUString& sColumnDef) return -1; } +OUString lcl_getDefaultValue(const OUString& sColumnDef) +{ + constexpr char DEFAULT_KW[] = "DEFAULT"; + auto nDefPos = sColumnDef.indexOf(DEFAULT_KW); + if (nDefPos > 0 && lcl_getAutoIncrementDefault(sColumnDef) < 0) + { + const OUString& fromDefault = sColumnDef.copy(nDefPos + sizeof(DEFAULT_KW)).trim(); + + // next word is the value + auto nNextSpace = fromDefault.indexOf(" "); + return nNextSpace > 0 ? fromDefault.copy(0, fromDefault.indexOf(" ")) : fromDefault; + } + return OUString{}; +} + bool lcl_isNullable(const OUString& sColumnDef) { if (sColumnDef.indexOf("NOT NULL") >= 0) @@ -277,7 +292,8 @@ void CreateStmtParser::parseColumnPart(const OUString& sColumnPart) ColumnDefinition aColDef(rColumnName, lcl_getDataTypeFromHsql(sTypeName), aParams, isPrimaryKey, lcl_getAutoIncrementDefault(sColumn), - lcl_isNullable(sColumn), bCaseInsensitive); + lcl_isNullable(sColumn), bCaseInsensitive, + lcl_getDefaultValue(sColumn)); m_aColumns.push_back(aColDef); } diff --git a/dbaccess/source/filter/hsqldb/fbcreateparser.cxx b/dbaccess/source/filter/hsqldb/fbcreateparser.cxx index 59f41c887368..768c7286185b 100644 --- a/dbaccess/source/filter/hsqldb/fbcreateparser.cxx +++ b/dbaccess/source/filter/hsqldb/fbcreateparser.cxx @@ -173,6 +173,16 @@ OUString FbCreateStmtParser::compose() const if (columnIter->isCaseInsensitive()) lcl_appendWithSpace(sSql, "COLLATE UNICODE_CI"); + const OUString& sDefaultVal = columnIter->getDefault(); + if (!sDefaultVal.isEmpty()) + { + lcl_appendWithSpace(sSql, "DEFAULT"); + if (sDefaultVal.equalsIgnoreAsciiCase("NOW")) + lcl_appendWithSpace(sSql, "\'NOW\'"); // Fb likes it single quoted + else + lcl_appendWithSpace(sSql, sDefaultVal); + } + ++columnIter; sSql.append(","); } |