diff options
Diffstat (limited to 'dbaccess/source/filter/hsqldb/fbcreateparser.cxx')
-rw-r--r-- | dbaccess/source/filter/hsqldb/fbcreateparser.cxx | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/dbaccess/source/filter/hsqldb/fbcreateparser.cxx b/dbaccess/source/filter/hsqldb/fbcreateparser.cxx new file mode 100644 index 000000000000..f69580423cf2 --- /dev/null +++ b/dbaccess/source/filter/hsqldb/fbcreateparser.cxx @@ -0,0 +1,163 @@ +/* -*- 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include "fbcreateparser.hxx" +#include "columndef.hxx" + +#include <com/sun/star/sdbc/DataType.hpp> +#include <rtl/ustrbuf.hxx> + +using namespace css::sdbc; + +namespace +{ +void lcl_appendWithSpace(OUStringBuffer& sBuff, const OUString& sStr) +{ + sBuff.append(" "); + sBuff.append(sStr); +} + +OUString lcl_DataTypetoFbTypeName(sal_Int32 eType) +{ + switch (eType) + { + case DataType::CHAR: + case DataType::BINARY: + return OUString("CHAR"); + case DataType::VARCHAR: + case DataType::VARBINARY: + return OUString("VARCHAR"); + case DataType::TINYINT: // no such type in Firebird + case DataType::SMALLINT: + return OUString("SMALLINT"); + case DataType::INTEGER: + return OUString("INTEGER"); + case DataType::BIGINT: + return OUString("BIGINT"); + case DataType::NUMERIC: + return OUString("NUMERIC"); + case DataType::DECIMAL: + return OUString("DECIMAL"); + case DataType::BOOLEAN: + return OUString("BOOLEAN"); + case DataType::LONGVARCHAR: + case DataType::LONGVARBINARY: + case DataType::CLOB: + case DataType::BLOB: + case DataType::OTHER: + return OUString("BLOB"); + case DataType::DATE: + return OUString("DATE"); + case DataType::TIME: + return OUString("TIME"); + case DataType::TIMESTAMP: + return OUString("TIMESTAMP"); + case DataType::DOUBLE: + return OUString("DOUBLE PRECISION"); + default: + assert(false); + return OUString(); + } +} + +OUString lcl_getTypeModifier(sal_Int32 eType) +{ + // TODO bind -9546 magic number to a common definition. It also appears + // in the connectivity modul. + switch (eType) + { + case DataType::CLOB: + case DataType::LONGVARCHAR: + return OUString("SUB_TYPE 1"); + case DataType::LONGVARBINARY: + return OUString("SUB_TYPE -9546"); + case DataType::BINARY: + case DataType::VARBINARY: + return OUString("CHARACTER SET OCTETS"); + default: + return OUString(); + } +} + +} // unnamed namespace + +namespace dbahsql +{ +OUString FbCreateStmtParser::compose() const +{ + OUStringBuffer sSql("CREATE TABLE "); + sSql.append(getTableName()); + + lcl_appendWithSpace(sSql, "("); + auto& rColumns = getColumnDef(); + auto columnIter = rColumns.cbegin(); + while (columnIter != rColumns.end()) + { + lcl_appendWithSpace(sSql, columnIter->getName()); + lcl_appendWithSpace(sSql, lcl_DataTypetoFbTypeName(columnIter->getDataType())); + + const std::vector<sal_Int32> params = columnIter->getParams(); + if (params.size() > 0) + { + sSql.append("("); + auto it = params.cbegin(); + while (it != params.end()) + { + sSql.append(OUString::number(*it)); + ++it; + if (it != params.end()) + sSql.append(","); + } + sSql.append(")"); // end of param declaration + } + + // special modifiers here, based on type (e.g. charset, subtype) + OUString sModifier = lcl_getTypeModifier(columnIter->getDataType()); + if (!sModifier.isEmpty()) + lcl_appendWithSpace(sSql, sModifier); + + // TODO autoincremental default value with "START WITH" + if (columnIter->isAutoIncremental()) + lcl_appendWithSpace(sSql, "GENERATED BY DEFAULT AS IDENTITY"); + else if (!columnIter->isNullable()) + lcl_appendWithSpace(sSql, "NOT NULL"); + + if (columnIter->isPrimaryKey()) + lcl_appendWithSpace(sSql, "PRIMARY KEY"); + + ++columnIter; + if (columnIter != rColumns.end()) + 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(); +} + +} // dbahsql + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |