From aa974a1b3798e04424623ad331e9f5a0ae01a34b Mon Sep 17 00:00:00 2001 From: Tamas Bunth Date: Mon, 4 Feb 2019 22:06:47 +0100 Subject: tdf#119502: dbahsql: tables without primary key MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit No "PRIMARY KEY" keyword is needed, when composing a parsed sql which did not contain any primary key definition. Change-Id: Ife8b898806edba41a52d47dc04b1170606ea8aae Reviewed-on: https://gerrit.libreoffice.org/67379 Tested-by: Jenkins Reviewed-by: Tamás Bunth --- dbaccess/source/filter/hsqldb/fbcreateparser.cxx | 38 ++++++++++++++++-------- dbaccess/source/filter/hsqldb/fbcreateparser.hxx | 1 + 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/dbaccess/source/filter/hsqldb/fbcreateparser.cxx b/dbaccess/source/filter/hsqldb/fbcreateparser.cxx index ad5fa6e65aa4..7a2e642670ae 100644 --- a/dbaccess/source/filter/hsqldb/fbcreateparser.cxx +++ b/dbaccess/source/filter/hsqldb/fbcreateparser.cxx @@ -106,6 +106,26 @@ OUString lcl_getTypeModifier(sal_Int32 eType) namespace dbahsql { +void FbCreateStmtParser::appendPrimaryKeyPart(OUStringBuffer& rSql) const +{ + const std::vector& sPrimaryKeys = getPrimaryKeys(); + if (sPrimaryKeys.empty()) + return; // no primary key specified + + rSql.append(","); + rSql.append("PRIMARY KEY("); + auto it = sPrimaryKeys.cbegin(); + while (it != sPrimaryKeys.end()) + { + rSql.append(*it); + ++it; + if (it != sPrimaryKeys.end()) + rSql.append(","); + } + + rSql.append(")"); // end of primary key declaration +} + void FbCreateStmtParser::ensureProperTableLengths() const { const std::vector& rColumns = getColumnDef(); @@ -119,7 +139,7 @@ OUString FbCreateStmtParser::compose() const OUStringBuffer sSql("CREATE TABLE "); sSql.append(getTableName()); - lcl_appendWithSpace(sSql, "("); + lcl_appendWithSpace(sSql, "("); // column declaration auto& rColumns = getColumnDef(); auto columnIter = rColumns.cbegin(); while (columnIter != rColumns.end()) @@ -184,21 +204,13 @@ OUString FbCreateStmtParser::compose() const } ++columnIter; - sSql.append(","); - } - - sSql.append("PRIMARY KEY("); - const std::vector& sPrimaryKeys = getPrimaryKeys(); - auto it = sPrimaryKeys.cbegin(); - while (it != sPrimaryKeys.end()) - { - sSql.append(*it); - ++it; - if (it != sPrimaryKeys.end()) + if (columnIter != rColumns.end()) sSql.append(","); } - sSql.append("))"); // end of column declaration and primary keys + appendPrimaryKeyPart(sSql); + + sSql.append(")"); // end of column declaration return sSql.makeStringAndClear(); } diff --git a/dbaccess/source/filter/hsqldb/fbcreateparser.hxx b/dbaccess/source/filter/hsqldb/fbcreateparser.hxx index 6f9aa5898d04..c90e05c3bdd8 100644 --- a/dbaccess/source/filter/hsqldb/fbcreateparser.hxx +++ b/dbaccess/source/filter/hsqldb/fbcreateparser.hxx @@ -18,6 +18,7 @@ class SAL_DLLPUBLIC_EXPORT FbCreateStmtParser : public CreateStmtParser { protected: void ensureProperTableLengths() const; + void appendPrimaryKeyPart(rtl::OUStringBuffer& rSql) const; public: /** -- cgit