nss: upgrade to release 3.73
[LibreOffice.git] / dbaccess / source / filter / hsqldb / fbcreateparser.cxx
blob4f8fb8667d1ecf30b73b65b68acf6473ea6e9265
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "fbcreateparser.hxx"
21 #include "columndef.hxx"
22 #include "utils.hxx"
24 #include <com/sun/star/sdbc/DataType.hpp>
26 #include <rtl/ustrbuf.hxx>
28 using namespace css::sdbc;
30 namespace
32 void lcl_appendWithSpace(OUStringBuffer& sBuff, const OUString& sStr)
34 sBuff.append(" ");
35 sBuff.append(sStr);
38 OUString lcl_DataTypetoFbTypeName(sal_Int32 eType)
40 switch (eType)
42 case DataType::CHAR:
43 case DataType::BINARY:
44 return "CHAR";
45 case DataType::VARCHAR:
46 case DataType::VARBINARY:
47 return "VARCHAR";
48 case DataType::TINYINT: // no such type in Firebird
49 case DataType::SMALLINT:
50 return "SMALLINT";
51 case DataType::INTEGER:
52 return "INTEGER";
53 case DataType::BIGINT:
54 return "BIGINT";
55 case DataType::NUMERIC:
56 return "NUMERIC";
57 case DataType::DECIMAL:
58 return "DECIMAL";
59 case DataType::BOOLEAN:
60 return "BOOLEAN";
61 case DataType::LONGVARCHAR:
62 case DataType::LONGVARBINARY:
63 case DataType::CLOB:
64 case DataType::BLOB:
65 case DataType::OTHER:
66 return "BLOB";
67 case DataType::DATE:
68 return "DATE";
69 case DataType::TIME:
70 return "TIME";
71 case DataType::TIMESTAMP:
72 return "TIMESTAMP";
73 case DataType::DOUBLE:
74 case DataType::REAL:
75 return "DOUBLE PRECISION";
76 case DataType::FLOAT:
77 return "FLOAT";
78 default:
79 assert(false);
80 return OUString();
84 OUString lcl_getTypeModifier(sal_Int32 eType)
86 // TODO bind -9546 magic number to a common definition. It also appears
87 // in the connectivity module.
88 switch (eType)
90 case DataType::CLOB:
91 case DataType::LONGVARCHAR:
92 return "SUB_TYPE 1";
93 case DataType::LONGVARBINARY:
94 return "SUB_TYPE -9546";
95 case DataType::BINARY:
96 case DataType::VARBINARY:
97 return "CHARACTER SET OCTETS";
98 default:
99 return OUString();
103 } // unnamed namespace
105 namespace dbahsql
107 void FbCreateStmtParser::appendPrimaryKeyPart(OUStringBuffer& rSql) const
109 const std::vector<OUString>& sPrimaryKeys = getPrimaryKeys();
110 if (sPrimaryKeys.empty())
111 return; // no primary key specified
113 rSql.append(",");
114 rSql.append("PRIMARY KEY(");
115 auto it = sPrimaryKeys.cbegin();
116 while (it != sPrimaryKeys.end())
118 rSql.append(*it);
119 ++it;
120 if (it != sPrimaryKeys.end())
121 rSql.append(",");
124 rSql.append(")"); // end of primary key declaration
127 void FbCreateStmtParser::ensureProperTableLengths() const
129 const std::vector<ColumnDefinition>& rColumns = getColumnDef();
130 for (const auto& col : rColumns)
131 utils::ensureFirebirdTableLength(col.getName());
134 OUString FbCreateStmtParser::compose() const
136 ensureProperTableLengths();
137 OUStringBuffer sSql(128);
138 sSql.append("CREATE TABLE ");
139 sSql.append(getTableName());
141 lcl_appendWithSpace(sSql, "("); // column declaration
142 auto& rColumns = getColumnDef();
143 auto columnIter = rColumns.cbegin();
144 while (columnIter != rColumns.end())
146 lcl_appendWithSpace(sSql, columnIter->getName());
147 lcl_appendWithSpace(sSql, lcl_DataTypetoFbTypeName(columnIter->getDataType()));
149 std::vector<sal_Int32> params{ columnIter->getParams() };
151 if (columnIter->getDataType() == DataType::NUMERIC
152 || columnIter->getDataType() == DataType::DECIMAL)
154 // max precision is 18 here
155 if (params.at(0) > 18)
156 params[0] = 18;
159 // Firebird SQL dialect does not like parameters for TIMESTAMP
160 if (!params.empty() && columnIter->getDataType() != DataType::TIMESTAMP)
162 sSql.append("(");
163 auto it = params.cbegin();
164 while (it != params.end())
166 sSql.append(OUString::number(*it));
167 ++it;
168 if (it != params.end())
169 sSql.append(",");
171 sSql.append(")"); // end of param declaration
174 // special modifiers here, based on type (e.g. charset, subtype)
175 OUString sModifier = lcl_getTypeModifier(columnIter->getDataType());
176 if (!sModifier.isEmpty())
177 lcl_appendWithSpace(sSql, sModifier);
179 if (columnIter->isAutoIncremental())
181 lcl_appendWithSpace(sSql, "GENERATED BY DEFAULT AS IDENTITY (START WITH ");
183 // start with 0:
184 // HSQLDB: first value will be 0.
185 // Firebird: first value will be 1.
186 sSql.append(columnIter->getStartValue() - 1);
187 sSql.append(")");
189 else if (!columnIter->isNullable())
190 lcl_appendWithSpace(sSql, "NOT NULL");
192 if (columnIter->isCaseInsensitive())
193 lcl_appendWithSpace(sSql, "COLLATE UNICODE_CI");
195 const OUString& sDefaultVal = columnIter->getDefault();
196 if (!sDefaultVal.isEmpty())
198 lcl_appendWithSpace(sSql, "DEFAULT");
199 if (sDefaultVal.equalsIgnoreAsciiCase("NOW"))
200 lcl_appendWithSpace(sSql, "\'NOW\'"); // Fb likes it single quoted
201 else
202 lcl_appendWithSpace(sSql, sDefaultVal);
205 ++columnIter;
206 if (columnIter != rColumns.end())
207 sSql.append(",");
210 appendPrimaryKeyPart(sSql);
212 sSql.append(")"); // end of column declaration
213 return sSql.makeStringAndClear();
216 } // dbahsql
218 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */