1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
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
;
21 constexpr std::size_t operator"" _z(unsigned long long n
) { return n
; }
23 const ColumnDefinition
* lcl_findByType(const std::vector
<ColumnDefinition
>& columns
,
26 for (const auto& column
: columns
)
28 if (column
.getDataType() == nType
)
35 class HsqlSchemaImportTest
: public CppUnit::TestFixture
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 const OUString sql
{ "CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL PRIMARY KEY)" };
66 FbCreateStmtParser aCreateParser
;
67 aCreateParser
.parse(sql
);
69 CPPUNIT_ASSERT_EQUAL(OUString
{ "\"myTable\"" }, aCreateParser
.getTableName());
70 const auto& columns
= aCreateParser
.getColumnDef();
71 CPPUNIT_ASSERT_EQUAL(1_z
, columns
.size());
72 const auto& column
= columns
.at(0);
73 CPPUNIT_ASSERT_EQUAL(OUString
{ "\"id\"" }, column
.getName());
74 CPPUNIT_ASSERT_EQUAL(css::sdbc::DataType::INTEGER
, column
.getDataType());
75 CPPUNIT_ASSERT(column
.isPrimaryKey());
76 CPPUNIT_ASSERT(!column
.isNullable());
77 CPPUNIT_ASSERT(!column
.isAutoIncremental());
80 void HsqlSchemaImportTest::testVarcharWithParam()
83 "CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL PRIMARY KEY, \"myText\" "
87 FbCreateStmtParser aCreateParser
;
88 aCreateParser
.parse(sql
);
90 const auto& columns
= aCreateParser
.getColumnDef();
91 CPPUNIT_ASSERT_EQUAL(2_z
, columns
.size());
92 const ColumnDefinition
* colVarchar
= lcl_findByType(columns
, css::sdbc::DataType::VARCHAR
);
93 CPPUNIT_ASSERT(colVarchar
!= nullptr);
94 const auto& params
= colVarchar
->getParams();
95 CPPUNIT_ASSERT_EQUAL(1_z
, params
.size());
96 constexpr sal_Int32 nParamExpected
= 50;
97 CPPUNIT_ASSERT_EQUAL(nParamExpected
, params
.at(0)); // VARCHAR(50)
102 * HSQLDB might define a column VARCHAR without parameter. With Firebird
103 * dialect, this is forbidden, so a default parameter has to be appended:
105 void HsqlSchemaImportTest::testVarcharWithoutParam()
108 "CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL PRIMARY KEY, \"myText\" "
112 FbCreateStmtParser aCreateParser
;
113 aCreateParser
.parse(sql
);
115 const auto& columns
= aCreateParser
.getColumnDef();
116 CPPUNIT_ASSERT_EQUAL(2_z
, columns
.size());
117 const ColumnDefinition
* colVarchar
= lcl_findByType(columns
, css::sdbc::DataType::VARCHAR
);
118 CPPUNIT_ASSERT(colVarchar
!= nullptr);
119 const auto& params
= colVarchar
->getParams();
120 CPPUNIT_ASSERT_EQUAL(1_z
, params
.size()); // parameter generated
123 void HsqlSchemaImportTest::testNumericWithTwoParam()
126 "CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL PRIMARY KEY, \"Betrag\" "
130 FbCreateStmtParser aCreateParser
;
131 aCreateParser
.parse(sql
);
133 const auto& columns
= aCreateParser
.getColumnDef();
134 CPPUNIT_ASSERT_EQUAL(2_z
, columns
.size());
136 const ColumnDefinition
* colNumeric
= lcl_findByType(columns
, css::sdbc::DataType::NUMERIC
);
137 CPPUNIT_ASSERT(colNumeric
!= nullptr);
138 const auto& params
= colNumeric
->getParams();
139 CPPUNIT_ASSERT_EQUAL(2_z
, params
.size());
141 constexpr sal_Int32 nPrecision
= 8;
142 constexpr sal_Int32 nScale
= 2;
143 CPPUNIT_ASSERT_EQUAL(nPrecision
, params
.at(0));
144 CPPUNIT_ASSERT_EQUAL(nScale
, params
.at(1));
147 void HsqlSchemaImportTest::testIntegerAutoincremental()
150 "CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL PRIMARY KEY GENERATED "
151 "BY DEFAULT AS IDENTITY(START WITH 0), \"myText\" VARCHAR(50))"
154 FbCreateStmtParser aCreateParser
;
155 aCreateParser
.parse(sql
);
157 const auto& columns
= aCreateParser
.getColumnDef();
158 const auto column
= columns
.at(0);
159 CPPUNIT_ASSERT_EQUAL(css::sdbc::DataType::INTEGER
, column
.getDataType());
160 CPPUNIT_ASSERT(column
.isAutoIncremental());
161 CPPUNIT_ASSERT(column
.isPrimaryKey());
162 CPPUNIT_ASSERT(!column
.isNullable());
167 * Hsqldb might use one parameter for defining column with type TIMESTAMP.
168 * With Firebird this is illegal.
170 void HsqlSchemaImportTest::testTimestampWithParam()
173 "CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL PRIMARY KEY, \"myText\" "
177 FbCreateStmtParser aCreateParser
;
178 aCreateParser
.parse(sql
);
180 const auto& columns
= aCreateParser
.getColumnDef();
181 const ColumnDefinition
* colTimeStamp
= lcl_findByType(columns
, css::sdbc::DataType::TIMESTAMP
);
183 CPPUNIT_ASSERT(colTimeStamp
!= nullptr);
185 // instead of asserting parameter size, look at the deparsed string,
186 // because it's Firebird specific
187 OUString fbSql
= aCreateParser
.compose();
188 CPPUNIT_ASSERT(fbSql
.indexOf("0") < 0); //does not contain
193 * HSQLDB uses keyword NOW without quotes. Firebird uses single quotes 'NOW'
195 void HsqlSchemaImportTest::testDefaultValueNow()
198 "CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL PRIMARY KEY, \"myDate\" "
199 "TIMESTAMP DEFAULT NOW)"
202 FbCreateStmtParser aCreateParser
;
203 aCreateParser
.parse(sql
);
205 const auto& columns
= aCreateParser
.getColumnDef();
206 const ColumnDefinition
* colTimeStamp
= lcl_findByType(columns
, css::sdbc::DataType::TIMESTAMP
);
208 CPPUNIT_ASSERT(colTimeStamp
!= nullptr);
209 CPPUNIT_ASSERT_EQUAL(OUString
{ "NOW" }, colTimeStamp
->getDefault()); // parsed NOW
210 OUString fbSql
= aCreateParser
.compose();
211 CPPUNIT_ASSERT(fbSql
.indexOf("\'NOW\'") > 0); // composed 'NOW'
214 void HsqlSchemaImportTest::testEvilNullColumnName()
216 const OUString sql
{ "CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL PRIMARY KEY, "
217 "\"myEvilNOT NULLName\" "
220 FbCreateStmtParser aCreateParser
;
221 aCreateParser
.parse(sql
);
223 const auto& columns
= aCreateParser
.getColumnDef();
224 CPPUNIT_ASSERT_EQUAL(2_z
, columns
.size());
225 const ColumnDefinition
* colVarchar
= lcl_findByType(columns
, css::sdbc::DataType::VARCHAR
);
226 CPPUNIT_ASSERT(colVarchar
!= nullptr);
227 CPPUNIT_ASSERT(colVarchar
->isNullable());
230 CPPUNIT_TEST_SUITE_REGISTRATION(HsqlSchemaImportTest
);
232 CPPUNIT_PLUGIN_IMPLEMENT();