Sqlite: Add condition to index

Change-Id: I83851b2f9cd516f21bc7e8987c1b60efaa019bb1
Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Marco Bubke
2021-06-10 15:48:22 +02:00
parent 66ba9c4843
commit b32f607bc7
4 changed files with 60 additions and 20 deletions

View File

@@ -43,14 +43,16 @@ enum class IndexType
class Index
{
public:
Index(Utils::SmallString &&tableName,
Index(Utils::SmallStringView tableName,
Utils::SmallStringVector &&columnNames,
IndexType indexType=IndexType::Normal)
: m_tableName(std::move(tableName)),
m_columnNames(std::move(columnNames)),
m_indexType(indexType)
{
}
IndexType indexType = IndexType::Normal,
Utils::SmallStringView condition = {})
: m_tableName(std::move(tableName))
, m_columnNames(std::move(columnNames))
, m_indexType(indexType)
, m_condition{condition}
{}
Utils::SmallString sqlStatement() const
{
@@ -67,7 +69,9 @@ public:
m_tableName,
"(",
m_columnNames.join(", "),
")"});
")",
m_condition.hasContent() ? " WHERE " : "",
m_condition});
}
void checkTableName() const
@@ -86,6 +90,7 @@ private:
Utils::SmallString m_tableName;
Utils::SmallStringVector m_columnNames;
IndexType m_indexType;
Utils::SmallString m_condition;
};
using SqliteIndices = std::vector<Index>;

View File

@@ -134,20 +134,21 @@ public:
m_tableConstraints.emplace_back(TablePrimaryKey{std::move(columnNames)});
}
Index &addIndex(const SqliteColumnConstReferences &columns)
Index &addIndex(const SqliteColumnConstReferences &columns, Utils::SmallStringView condition = {})
{
m_sqliteIndices.emplace_back(m_tableName.clone(), sqliteColumnNames(columns));
return m_sqliteIndices.back();
return m_sqliteIndices.emplace_back(m_tableName,
sqliteColumnNames(columns),
IndexType::Normal,
condition);
}
Index &addUniqueIndex(const SqliteColumnConstReferences &columns)
Index &addUniqueIndex(const SqliteColumnConstReferences &columns,
Utils::SmallStringView condition = {})
{
m_sqliteIndices.emplace_back(m_tableName.clone(),
sqliteColumnNames(columns),
IndexType::Unique);
return m_sqliteIndices.back();
return m_sqliteIndices.emplace_back(m_tableName,
sqliteColumnNames(columns),
IndexType::Unique,
condition);
}
const SqliteColumns &columns() const

View File

@@ -73,4 +73,15 @@ TEST(Index, UniqueIndex)
ASSERT_THAT(sqlStatement, Eq("CREATE UNIQUE INDEX IF NOT EXISTS index_tableName_column1 ON tableName(column1)"));
}
TEST(Index, Condition)
{
Index index{"tableName", {"column1"}, IndexType::Normal, "column1 IS NOT NULL"};
auto sqlStatement = index.sqlStatement();
ASSERT_THAT(sqlStatement,
Eq("CREATE INDEX IF NOT EXISTS index_tableName_column1 ON tableName(column1) WHERE "
"column1 IS NOT NULL"));
}
}

View File

@@ -107,11 +107,34 @@ TEST_F(SqliteTable, InitializeTableWithIndex)
auto &column = table.addColumn("name");
auto &column2 = table.addColumn("value");
table.addIndex({column});
table.addIndex({column2});
table.addIndex({column2}, "value IS NOT NULL");
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_value ON testTable(value)")));
EXPECT_CALL(databaseMock,
execute(Eq("CREATE INDEX IF NOT EXISTS index_testTable_value ON testTable(value) "
"WHERE value IS NOT NULL")));
table.initialize(databaseMock);
}
TEST_F(SqliteTable, InitializeTableWithUniqueIndex)
{
InSequence sequence;
table.setName(tableName.clone());
auto &column = table.addColumn("name");
auto &column2 = table.addColumn("value");
table.addUniqueIndex({column});
table.addUniqueIndex({column2}, "value IS NOT NULL");
EXPECT_CALL(databaseMock, execute(Eq("CREATE TABLE testTable(name, value)")));
EXPECT_CALL(databaseMock,
execute(Eq(
"CREATE UNIQUE INDEX IF NOT EXISTS index_testTable_name ON testTable(name)")));
EXPECT_CALL(databaseMock,
execute(Eq(
"CREATE UNIQUE INDEX IF NOT EXISTS index_testTable_value ON testTable(value) "
"WHERE value IS NOT NULL")));
table.initialize(databaseMock);
}