1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
#pragma once
#include "sqliteglobal.h"
#include "sqliteexception.h"
#include <utils/smallstringvector.h>
namespace Sqlite {
enum class IndexType
{
Normal,
Unique
};
class Index
{
public:
Index(Utils::SmallStringView tableName,
Utils::SmallStringVector &&columnNames,
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
{
checkTableName();
checkColumns();
return Utils::SmallString::join({"CREATE ",
m_indexType == IndexType::Unique ? "UNIQUE " : "",
"INDEX IF NOT EXISTS index_",
m_tableName,
"_",
m_columnNames.join("_"),
" ON ",
m_tableName,
"(",
m_columnNames.join(", "),
")",
m_condition.hasContent() ? " WHERE " : "",
m_condition});
}
void checkTableName() const
{
if (m_tableName.isEmpty())
throw Exception("SqliteIndex has not table name!");
}
void checkColumns() const
{
if (m_columnNames.empty())
throw Exception("SqliteIndex has no columns!");
}
private:
Utils::SmallString m_tableName;
Utils::SmallStringVector m_columnNames;
IndexType m_indexType;
Utils::SmallString m_condition;
};
using SqliteIndices = std::vector<Index>;
} //
|