LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / dbaccess / qa / extras / hsql_schema_import.cxx
blob6d714ae9a8ae6f89af013d3670467d508c84f79e
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/.
8 */
10 #include <fbcreateparser.hxx>
11 #include <columndef.hxx>
12 #include <cppunit/plugin/TestPlugIn.h>
13 #include <com/sun/star/sdbc/DataType.hpp>
14 #include <cppunit/TestFixture.h>
15 #include <cppunit/extensions/HelperMacros.h>
17 using namespace dbahsql;
19 namespace
21 constexpr std::size_t operator"" _z(unsigned long long n) { return n; }
23 const ColumnDefinition* lcl_findByType(const std::vector<ColumnDefinition>& columns,
24 sal_Int32 nType)
26 for (const auto& column : columns)
28 if (column.getDataType() == nType)
29 return &column;
31 return nullptr;
35 class HsqlSchemaImportTest : public CppUnit::TestFixture
37 public:
38 void testIntegerPrimaryKeyNotNull();
39 void testVarcharWithParam();
40 void testVarcharWithoutParam();
41 void testNumericWithTwoParam();
42 void testIntegerAutoincremental();
43 void testTimestampWithParam();
44 void testDefaultValueNow();
45 void testEvilNullColumnName();
46 // TODO testForeign, testDecomposer
48 CPPUNIT_TEST_SUITE(HsqlSchemaImportTest);
50 CPPUNIT_TEST(testIntegerPrimaryKeyNotNull);
51 CPPUNIT_TEST(testVarcharWithParam);
52 CPPUNIT_TEST(testVarcharWithoutParam);
53 CPPUNIT_TEST(testNumericWithTwoParam);
54 CPPUNIT_TEST(testIntegerAutoincremental);
55 CPPUNIT_TEST(testTimestampWithParam);
56 CPPUNIT_TEST(testDefaultValueNow);
57 CPPUNIT_TEST(testEvilNullColumnName);
59 CPPUNIT_TEST_SUITE_END();
62 void HsqlSchemaImportTest::testIntegerPrimaryKeyNotNull()
64 FbCreateStmtParser aCreateParser;
65 aCreateParser.parse("CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL PRIMARY KEY)");
67 CPPUNIT_ASSERT_EQUAL(OUString{ "\"myTable\"" }, aCreateParser.getTableName());
68 const auto& columns = aCreateParser.getColumnDef();
69 CPPUNIT_ASSERT_EQUAL(1_z, columns.size());
70 const auto& column = columns.at(0);
71 CPPUNIT_ASSERT_EQUAL(OUString{ "\"id\"" }, column.getName());
72 CPPUNIT_ASSERT_EQUAL(css::sdbc::DataType::INTEGER, column.getDataType());
73 CPPUNIT_ASSERT(column.isPrimaryKey());
74 CPPUNIT_ASSERT(!column.isNullable());
75 CPPUNIT_ASSERT(!column.isAutoIncremental());
78 void HsqlSchemaImportTest::testVarcharWithParam()
80 FbCreateStmtParser aCreateParser;
81 aCreateParser.parse(
82 "CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL PRIMARY KEY, \"myText\" "
83 "VARCHAR(50))");
85 const auto& columns = aCreateParser.getColumnDef();
86 CPPUNIT_ASSERT_EQUAL(2_z, columns.size());
87 const ColumnDefinition* colVarchar = lcl_findByType(columns, css::sdbc::DataType::VARCHAR);
88 CPPUNIT_ASSERT(colVarchar != nullptr);
89 const auto& params = colVarchar->getParams();
90 CPPUNIT_ASSERT_EQUAL(1_z, params.size());
91 constexpr sal_Int32 nParamExpected = 50;
92 CPPUNIT_ASSERT_EQUAL(nParamExpected, params.at(0)); // VARCHAR(50)
95 /**
96 * Special case:
97 * HSQLDB might define a column VARCHAR without parameter. With Firebird
98 * dialect, this is forbidden, so a default parameter has to be appended:
99 **/
100 void HsqlSchemaImportTest::testVarcharWithoutParam()
102 FbCreateStmtParser aCreateParser;
103 aCreateParser.parse(
104 "CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL PRIMARY KEY, \"myText\" "
105 "VARCHAR)");
107 const auto& columns = aCreateParser.getColumnDef();
108 CPPUNIT_ASSERT_EQUAL(2_z, columns.size());
109 const ColumnDefinition* colVarchar = lcl_findByType(columns, css::sdbc::DataType::VARCHAR);
110 CPPUNIT_ASSERT(colVarchar != nullptr);
111 const auto& params = colVarchar->getParams();
112 CPPUNIT_ASSERT_EQUAL(1_z, params.size()); // parameter generated
115 void HsqlSchemaImportTest::testNumericWithTwoParam()
117 FbCreateStmtParser aCreateParser;
118 aCreateParser.parse(
119 "CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL PRIMARY KEY, \"Betrag\" "
120 "NUMERIC(8,2))");
122 const auto& columns = aCreateParser.getColumnDef();
123 CPPUNIT_ASSERT_EQUAL(2_z, columns.size());
125 const ColumnDefinition* colNumeric = lcl_findByType(columns, css::sdbc::DataType::NUMERIC);
126 CPPUNIT_ASSERT(colNumeric != nullptr);
127 const auto& params = colNumeric->getParams();
128 CPPUNIT_ASSERT_EQUAL(2_z, params.size());
130 constexpr sal_Int32 nPrecision = 8;
131 constexpr sal_Int32 nScale = 2;
132 CPPUNIT_ASSERT_EQUAL(nPrecision, params.at(0));
133 CPPUNIT_ASSERT_EQUAL(nScale, params.at(1));
136 void HsqlSchemaImportTest::testIntegerAutoincremental()
138 FbCreateStmtParser aCreateParser;
139 aCreateParser.parse(
140 "CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL PRIMARY KEY GENERATED "
141 "BY DEFAULT AS IDENTITY(START WITH 0), \"myText\" VARCHAR(50))");
143 const auto& columns = aCreateParser.getColumnDef();
144 const auto column = columns.at(0);
145 CPPUNIT_ASSERT_EQUAL(css::sdbc::DataType::INTEGER, column.getDataType());
146 CPPUNIT_ASSERT(column.isAutoIncremental());
147 CPPUNIT_ASSERT(column.isPrimaryKey());
148 CPPUNIT_ASSERT(!column.isNullable());
152 * Special case:
153 * Hsqldb might use one parameter for defining column with type TIMESTAMP.
154 * With Firebird this is illegal.
156 void HsqlSchemaImportTest::testTimestampWithParam()
158 FbCreateStmtParser aCreateParser;
159 aCreateParser.parse(
160 "CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL PRIMARY KEY, \"myText\" "
161 "TIMESTAMP(0))");
163 const auto& columns = aCreateParser.getColumnDef();
164 const ColumnDefinition* colTimeStamp = lcl_findByType(columns, css::sdbc::DataType::TIMESTAMP);
166 CPPUNIT_ASSERT(colTimeStamp != nullptr);
168 // instead of asserting parameter size, look at the deparsed string,
169 // because it's Firebird specific
170 OUString fbSql = aCreateParser.compose();
171 CPPUNIT_ASSERT(fbSql.indexOf("0") < 0); //does not contain
175 * Special case:
176 * HSQLDB uses keyword NOW without quotes. Firebird uses single quotes 'NOW'
178 void HsqlSchemaImportTest::testDefaultValueNow()
180 FbCreateStmtParser aCreateParser;
181 aCreateParser.parse(
182 "CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL PRIMARY KEY, \"myDate\" "
183 "TIMESTAMP DEFAULT NOW)");
185 const auto& columns = aCreateParser.getColumnDef();
186 const ColumnDefinition* colTimeStamp = lcl_findByType(columns, css::sdbc::DataType::TIMESTAMP);
188 CPPUNIT_ASSERT(colTimeStamp != nullptr);
189 CPPUNIT_ASSERT_EQUAL(OUString{ "NOW" }, colTimeStamp->getDefault()); // parsed NOW
190 OUString fbSql = aCreateParser.compose();
191 CPPUNIT_ASSERT(fbSql.indexOf("\'NOW\'") > 0); // composed 'NOW'
194 void HsqlSchemaImportTest::testEvilNullColumnName()
196 FbCreateStmtParser aCreateParser;
197 aCreateParser.parse("CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL PRIMARY KEY, "
198 "\"myEvilNOT NULLName\" "
199 "VARCHAR(20))");
201 const auto& columns = aCreateParser.getColumnDef();
202 CPPUNIT_ASSERT_EQUAL(2_z, columns.size());
203 const ColumnDefinition* colVarchar = lcl_findByType(columns, css::sdbc::DataType::VARCHAR);
204 CPPUNIT_ASSERT(colVarchar != nullptr);
205 CPPUNIT_ASSERT(colVarchar->isNullable());
208 CPPUNIT_TEST_SUITE_REGISTRATION(HsqlSchemaImportTest);
210 CPPUNIT_PLUGIN_IMPLEMENT();