Sqlite: Default column type to None

None has no type affinity. So it will not try to convert types to other
types like Numeric will do.

Change-Id: I5362a6d071459594619485b8b8a37b3f5ed12db6
Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
Marco Bubke
2021-04-09 09:49:14 +02:00
parent 385158ddcc
commit 8edbdd78ad
7 changed files with 28 additions and 20 deletions

View File

@@ -248,7 +248,9 @@ void CreateTableSqlStatementBuilder::bindColumnDefinitionsAndTableConstraints()
columnDefinitionStrings.reserve(m_columns.size()); columnDefinitionStrings.reserve(m_columns.size());
for (const Column &column : m_columns) { for (const Column &column : m_columns) {
Utils::SmallString columnDefinitionString = {column.name, " ", column.typeString()}; Utils::SmallString columnDefinitionString = {column.name,
SqlStatementBuilder::columnTypeToString(
column.type)};
ContraintsVisiter visiter{columnDefinitionString}; ContraintsVisiter visiter{columnDefinitionString};

View File

@@ -39,7 +39,7 @@ public:
Column(Utils::SmallStringView tableName, Column(Utils::SmallStringView tableName,
Utils::SmallStringView name, Utils::SmallStringView name,
ColumnType type, ColumnType type = ColumnType::None,
Constraints &&constraints = {}) Constraints &&constraints = {})
: constraints(std::move(constraints)) : constraints(std::move(constraints))
, name(name) , name(name)

View File

@@ -39,7 +39,7 @@
namespace Sqlite { namespace Sqlite {
enum class ColumnType : char { Numeric, Integer, Real, Text, Blob, None }; enum class ColumnType : char { None, Numeric, Integer, Real, Text, Blob };
enum class ConstraintType : char { NoConstraint, PrimaryKey, Unique, ForeignKey }; enum class ConstraintType : char { NoConstraint, PrimaryKey, Unique, ForeignKey };

View File

@@ -72,7 +72,7 @@ public:
} }
Column &addColumn(Utils::SmallStringView name, Column &addColumn(Utils::SmallStringView name,
ColumnType type = ColumnType::Numeric, ColumnType type = ColumnType::None,
Constraints &&constraints = {}) Constraints &&constraints = {})
{ {
m_sqliteColumns.emplace_back(m_tableName, name, type, std::move(constraints)); m_sqliteColumns.emplace_back(m_tableName, name, type, std::move(constraints));

View File

@@ -179,12 +179,18 @@ bool SqlStatementBuilder::isBuild() const
Utils::SmallString SqlStatementBuilder::columnTypeToString(ColumnType columnType) Utils::SmallString SqlStatementBuilder::columnTypeToString(ColumnType columnType)
{ {
switch (columnType) { switch (columnType) {
case ColumnType::Numeric: return "NUMERIC"; case ColumnType::Numeric:
case ColumnType::Integer: return "INTEGER"; return " NUMERIC";
case ColumnType::Real: return "REAL"; case ColumnType::Integer:
case ColumnType::Text: return "TEXT"; return " INTEGER";
case ColumnType::Blob: return "BLOB"; case ColumnType::Real:
case ColumnType::None: return {}; return " REAL";
case ColumnType::Text:
return " TEXT";
case ColumnType::Blob:
return " BLOB";
case ColumnType::None:
return {};
} }
Q_UNREACHABLE(); Q_UNREACHABLE();

View File

@@ -93,7 +93,9 @@ TEST_F(SqliteTable, InitializeTable)
table.addColumn("name"); table.addColumn("name");
table.addColumn("value"); table.addColumn("value");
EXPECT_CALL(databaseMock, execute(Eq("CREATE TEMPORARY TABLE IF NOT EXISTS testTable(name NUMERIC, value NUMERIC) WITHOUT ROWID"))); EXPECT_CALL(databaseMock,
execute(Eq(
"CREATE TEMPORARY TABLE IF NOT EXISTS testTable(name, value) WITHOUT ROWID")));
table.initialize(databaseMock); table.initialize(databaseMock);
} }
@@ -107,7 +109,7 @@ TEST_F(SqliteTable, InitializeTableWithIndex)
table.addIndex({column}); table.addIndex({column});
table.addIndex({column2}); table.addIndex({column2});
EXPECT_CALL(databaseMock, execute(Eq("CREATE TABLE testTable(name NUMERIC, value NUMERIC)"))); EXPECT_CALL(databaseMock, execute(Eq("CREATE TABLE testTable(name, value)")));
EXPECT_CALL(databaseMock, execute(Eq("CREATE INDEX IF NOT EXISTS index_testTable_name ON testTable(name)"))); EXPECT_CALL(databaseMock, execute(Eq("CREATE INDEX IF NOT EXISTS index_testTable_name ON testTable(name)")));
EXPECT_CALL(databaseMock, execute(Eq("CREATE INDEX IF NOT EXISTS index_testTable_value ON testTable(value)"))); EXPECT_CALL(databaseMock, execute(Eq("CREATE INDEX IF NOT EXISTS index_testTable_value ON testTable(value)")));
@@ -299,9 +301,7 @@ TEST_F(SqliteTable, AddPrimaryTableContraint)
const auto &nameColumn = table.addColumn("name"); const auto &nameColumn = table.addColumn("name");
table.addPrimaryKeyContraint({idColumn, nameColumn}); table.addPrimaryKeyContraint({idColumn, nameColumn});
EXPECT_CALL(databaseMock, EXPECT_CALL(databaseMock, execute(Eq("CREATE TABLE testTable(id, name, PRIMARY KEY(id, name))")));
execute(
Eq("CREATE TABLE testTable(id NUMERIC, name NUMERIC, PRIMARY KEY(id, name))")));
table.initialize(databaseMock); table.initialize(databaseMock);
} }